简体   繁体   中英

How to add claims during user registration

I'm using ASP.NET MVC 5 project with identity 2.1.0 and VS2013 U4. I want to add claims to user during registration in order to be stored in db. These claims represent user custom properties.
As I created a web page for administrator to create/edit/delete users, I'm still using create method from AccountController to create a user, but I don't want to login that user. How can I add those claims to the user ?

You probably already have a UserManager class. You can use that one to create users and to add claims.

As an example in a controller:

// gather some context stuff
var context = this.Request.GetContext();

// gather the user manager
var usermanager = context.Get<ApplicationUserManager>();

// add a country claim (given you have the userId)
usermanager.AddClaim("userid", new Claim(ClaimTypes.Country, "Germany"));

In order for this to work you need to implement your own UserManager and link it with the OWIN context (in the example it's ApplicationUserManager which basically is class ApplicationUserManager : UserManager<ApplicationUser> { } with only a small amount of configuration added). A bit of reading is available here: https://msdn.microsoft.com/en-us/library/dn613290%28v=vs.108%29.aspx

you can use Like

private void SignInAsync(User User)
{
    var claims = new List<Claim>();

    claims.Add(new Claim(ClaimTypes.Name, User.Employee.Name));
    claims.Add(new Claim(ClaimTypes.Email, User.Employee.EmailId));
    claims.Add(new Claim(ClaimTypes.Role, User.RoleId.ToString()));
    var id = new ClaimsIdentity(claims,
                                DefaultAuthenticationTypes.ApplicationCookie);
    var claimsPrincipal = new ClaimsPrincipal(id);
    // Set current principal
    Thread.CurrentPrincipal = claimsPrincipal;
    var ctx = Request.GetOwinContext();
    var authenticationManager = ctx.Authentication;

    authenticationManager.SignIn(id);
}

after login pass the User table value in this function

 SignInAsync(result);

you can get clam value like

var identity = (ClaimsPrincipal)Thread.CurrentPrincipal;
 // Get the claims values
        string UserRoleValue = identity.Claims.Where(c => c.Type == ClaimTypes.Role)
                           .Select(c => c.Value).SingleOrDefault();

You can, in fact, create claims at the same time you create the user account.

Just add the claims to the user object before you call CreateAsync on the user manager.

var identityUser = new IdentityUser
{
  UserName = username,
  Email = email,
  // etc...
  Claims = { new IdentityUserClaim { ClaimType = "SomeClaimType", ClaimValue = "SomeClaimValue"} }
};
var identityResult = await _userManager.CreateAsync(identityUser, password);

This will create the user and associate the claims with the user as one logical operation with persistence.

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