使用Web Api / OWIN Auth Token时的额外信息

时间:2022-09-02 16:53:15

I'm using out-of-the-box auth with Individual User Accounts that comes with the Visual Studio template for Web Api. I consume the api in an Angular.js front end.

我正在使用开箱即用的auth,其中包含用于Web Api的Visual Studio模板附带的个人用户帐户。我在Angular.js前端消费了api。

Not surprisingly, I need to store extra information about the user like first and last names. I would like to set set these extra values in the Register method.

毫不奇怪,我需要存储有关用户的额外信息,如名字和姓氏。我想在Register方法中设置这些额外的值。

Q. Should I extend the user model or should I store this information as claims?

问:我应该扩展用户模型还是应该将此信息存储为声明?

It would be a bonus if this information was 'automatically' returned in the response from /Token without adding extra code.

如果在/ Token的响应中“自动”返回此信息而不添加额外代码,那将是一个奖励。

1 个解决方案

#1


0  

I decided to return the extra info in the response to the /Token call.

我决定在/ Token调用的响应中返回额外的信息。

In ApplicationOAuthProvider (which is part of the template project) I changed CreateProperties and adjusted calls to CreateProperties in 2 places to pass the user, not just the username:

在ApplicationOAuthProvider(它是模板项目的一部分)中,我更改了CreateProperties,并在2个位置调整了对CreateProperties的调用以传递用户,而不仅仅是用户名:

    public static AuthenticationProperties CreateProperties(ApplicationUser user)
            {
                var firstNameClaim = user.Claims.FirstOrDefault(c => c.ClaimType == ClaimTypes.GivenName);
                var lastNameClaim = user.Claims.FirstOrDefault(c => c.ClaimType == ClaimTypes.Surname);
                var roles = user.Claims.Where(c => c.ClaimType == ClaimTypes.Role).Select(c => c.ClaimValue);

                IDictionary<string, string> data = new Dictionary<string, string>
                {
                    { "userName", user.UserName },
                    {"firstName", firstNameClaim != null ? firstNameClaim.ClaimValue : "" },
                    {"lastName", lastNameClaim != null ? lastNameClaim.ClaimValue : "" },
                    {"roles", string.Join( ",", roles) }
                };

                return new AuthenticationProperties(data);
            }

#1


0  

I decided to return the extra info in the response to the /Token call.

我决定在/ Token调用的响应中返回额外的信息。

In ApplicationOAuthProvider (which is part of the template project) I changed CreateProperties and adjusted calls to CreateProperties in 2 places to pass the user, not just the username:

在ApplicationOAuthProvider(它是模板项目的一部分)中,我更改了CreateProperties,并在2个位置调整了对CreateProperties的调用以传递用户,而不仅仅是用户名:

    public static AuthenticationProperties CreateProperties(ApplicationUser user)
            {
                var firstNameClaim = user.Claims.FirstOrDefault(c => c.ClaimType == ClaimTypes.GivenName);
                var lastNameClaim = user.Claims.FirstOrDefault(c => c.ClaimType == ClaimTypes.Surname);
                var roles = user.Claims.Where(c => c.ClaimType == ClaimTypes.Role).Select(c => c.ClaimValue);

                IDictionary<string, string> data = new Dictionary<string, string>
                {
                    { "userName", user.UserName },
                    {"firstName", firstNameClaim != null ? firstNameClaim.ClaimValue : "" },
                    {"lastName", lastNameClaim != null ? lastNameClaim.ClaimValue : "" },
                    {"roles", string.Join( ",", roles) }
                };

                return new AuthenticationProperties(data);
            }