Milan M.
Milan M.

Reputation: 1036

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 ?

Upvotes: 15

Views: 35935

Answers (3)

user3966829
user3966829

Reputation:

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 identity = new ClaimsIdentity(claims,
                                DefaultAuthenticationTypes.ApplicationCookie);
    var claimsPrincipal = new ClaimsPrincipal(identity);
    // Set current principal
    Thread.CurrentPrincipal = claimsPrincipal;
    var ctx = Request.GetOwinContext();
    var authenticationManager = ctx.Authentication;
    
    authenticationManager.SignIn(identity);
}

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();

Upvotes: 10

Cliff
Cliff

Reputation: 251

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.

Upvotes: 3

pysco68
pysco68

Reputation: 1146

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

Upvotes: 18

Related Questions