locked
How to add multiple claims when user login RRS feed

  • Question

  • User264732274 posted

    i read little about identity and i think multiple claims can be added and all claims stored in owin generated cookie and dropped in user pc.

    suppose if i store my claim for last pwd change date during login so when user request for any other page then how could i read my custom claim value and compare user last pwd change date and the last pwd change date value stored in db . if both are not same then redirect user to login page.

    1) show me how could i add multiple claims after validate user credential during login

    2) for next subsequent request how could i read back my claim data ?

    i am looking for good example to complete my jobs. thanks

    Tuesday, September 20, 2016 1:53 PM

Answers

  • User283571144 posted

    Hi sudip_inn,

    1) show me how could i add multiple claims after validate user credential during login

    2) for next subsequent request how could i read back my claim data ?

    As far as I know, asp.net identity support claims-based identity.

    After adding the claims-based identity, you could use ClaimsIdentity.AddClaim method to add the claim.

    You could add multi claim here.

    For example:

    UserManager<applicationuser> userManager = new UserManager<applicationuser>(new UserStore<applicationuser>(new SecurityContext()));
    ClaimsIdentity identity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie
    var user = userManager.Find(userName, password);
    identity.AddClaim(new Claim("Email", user.Email));
    identity.AddClaim(new Claim("Email2", user.Email));
    

    More details, you could refer to follow link:

    http://kevin-junghans.blogspot.sg/2013/12/using-claims-in-aspnet-identity.html

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 21, 2016 9:54 AM
  • User283571144 posted

    Hi sudip_inn,

    1) now question is how could i read back the email data from claim.......if possible show me the code

    I suggest you could use UserManager.GetClaims method to get the claim.

    More details, you could refer to follow codes:

                var userId = User.Identity.GetUserId();
                ApplicationUserManager UserManager = Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
                var claims = UserManager.GetClaims(userId);// get this user all claims
                var someClaim = claims.FirstOrDefault(c => c.Type == "E-mail");//get the claim type is e-mail
    

    2) claim data stored in auth cookie ? is it mandatory that always claim data has to stored in auth cookie ?

    3) user has to write code to store claims or claims data to auth cookie or does it managed by asp.net engine automatically ?

    Yes, it will automatically store the claim data into auth coookie.

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 25, 2016 8:15 AM

All replies

  • User283571144 posted

    Hi sudip_inn,

    1) show me how could i add multiple claims after validate user credential during login

    2) for next subsequent request how could i read back my claim data ?

    As far as I know, asp.net identity support claims-based identity.

    After adding the claims-based identity, you could use ClaimsIdentity.AddClaim method to add the claim.

    You could add multi claim here.

    For example:

    UserManager<applicationuser> userManager = new UserManager<applicationuser>(new UserStore<applicationuser>(new SecurityContext()));
    ClaimsIdentity identity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie
    var user = userManager.Find(userName, password);
    identity.AddClaim(new Claim("Email", user.Email));
    identity.AddClaim(new Claim("Email2", user.Email));
    

    More details, you could refer to follow link:

    http://kevin-junghans.blogspot.sg/2013/12/using-claims-in-aspnet-identity.html

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 21, 2016 9:54 AM
  • User264732274 posted

    @Brando u show this way we can store or add data to claim

    identity.AddClaim(new Claim("Email", user.Email));

    1) now question is how could i read back the email data from claim.......if possible show me the code

    2) claim data stored in auth cookie ? is it mandatory that always claim data has to stored in auth cookie ?

    3) user has to write code to store claims or claims data to auth cookie or does it managed by asp.net engine automatically ?

    Wednesday, September 21, 2016 1:13 PM
  • User283571144 posted

    Hi sudip_inn,

    1) now question is how could i read back the email data from claim.......if possible show me the code

    I suggest you could use UserManager.GetClaims method to get the claim.

    More details, you could refer to follow codes:

                var userId = User.Identity.GetUserId();
                ApplicationUserManager UserManager = Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
                var claims = UserManager.GetClaims(userId);// get this user all claims
                var someClaim = claims.FirstOrDefault(c => c.Type == "E-mail");//get the claim type is e-mail
    

    2) claim data stored in auth cookie ? is it mandatory that always claim data has to stored in auth cookie ?

    3) user has to write code to store claims or claims data to auth cookie or does it managed by asp.net engine automatically ?

    Yes, it will automatically store the claim data into auth coookie.

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 25, 2016 8:15 AM