简体   繁体   中英

ASP.NET Core MVC custom identity properties

I am trying to build a website in ASP.NET Core MVC and am using the Microsoft.Identity library. I have a custom property in my User (ApplicationUser) class which is called Token . I want to create a cookie on login with that token. So I need to call some function that allows me to fetch the Token attribute from the logged in user (via UserManager or whatever. It has to be the user that logged in.)

I have searched on the internet and have found several solutions by creating a custom Factory and then adding it to the startup.cs Like this . But I cannot find or see a way to access the property. User.Identity.GetToken() does not work.

Here is my custom factory:

 public class CustomUserIdentityFactory : UserClaimsPrincipalFactory<User, IdentityRole>
    {
        public CustomUserIdentityFactory(UserManager<User> userManager, RoleManager<IdentityRole> roleManager, IOptions<IdentityOptions> optionsAccessor) : base(userManager, roleManager, optionsAccessor)
        {}

        public override async Task<ClaimsPrincipal> CreateAsync(User user) {
            var principal = await base.CreateAsync(user);

            if(!string.IsNullOrWhiteSpace(user.Token)) {
                ((ClaimsIdentity)principal.Identity).AddClaims(new[] {
                    new Claim(ClaimTypes.Hash, user.Token)
                });
            }

            return principal;
        }
    }

Here is the configure in my Startup.cs

services.AddScoped<IUserClaimsPrincipalFactory<User>, CustomUserIdentityFactory>();

So, long story short: I am trying to access a custom identity property and have found a way to add it to the UserManager, but can not find a way to access it.

Your "CustomUserIdentityFactory" adding claims to the logged in user, so that claims will be added in to the cookie, which can be accessed using "User.Claims" by specifying your claim type.

Assume your claim type is " http://www.example.com/ws/identity/claims/v1/token "

Change your code as below by overriding "CreateAsync" method using your own claim type.

public override async Task<ClaimsPrincipal> CreateAsync(User user) {
            var principal = await base.CreateAsync(user);
            var tokenClaimType = "http://www.example.com/ws/identity/claims/v1/token"

            if(!string.IsNullOrWhiteSpace(user.Token)) {
                ((ClaimsIdentity)principal.Identity).AddClaims(new[] {
                    new Claim(tokenClaimType, user.Token)
                });
            }

            return principal;
        }

How to access token as part of "User.Claims"

var tokenClaimType = "http://www.example.com/ws/identity/claims/v1/token"
var token = User.Claims.Where(claim => claim.Type == tokenClaimType);

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM