2

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.

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.

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.

tmg
  • 19,895
  • 5
  • 72
  • 76
tymtam
  • 31,798
  • 8
  • 86
  • 126
  • 1
    Found this: http://stackoverflow.com/questions/26357054/return-more-info-to-the-client-using-oauth-bearer-tokens-generation-and-owin-in – Marcus Höglund Jun 22 '16 at 09:14

1 Answers1

0

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

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:

    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);
            }
tymtam
  • 31,798
  • 8
  • 86
  • 126