Dynamic user queries in ASP.NET Identity EF

I am working on an authentication system that uses an ASP.NET identifier with the Entity Framework, and I want to have a few statements that are computed values, not hard-coded into the claims table.

When a user logs in, how can I add dynamic requirements to this login session without adding them to the claims table?

For example, I might want to save each user DOB, but I want to add IsBirthday as a claim if the login date matches the user DOB. I do not want to store the IsBirthday application for each user, as it changes daily for everyone.

In my code, I use this to login:

var signInResult = await SignInManager.PasswordSignInAsync(username, password, false, false); 

After this is called, I can reference the ClaimsPrincipal, but the Claims property is IEnumerable, not List, so I cannot add it.

EDIT: I should also mention that I use the Microsoft.AspNet.Identity.Owin libraries.

+6
source share
1 answer

OK, that’s it, I moved a little to the classes represented in ASP.NET Identity and found the one I needed to override. The SignInManager class has a CreateUserIdentityAsync method that does exactly what I wanted. The following code added an IsBirthday application to my identity, but did not save it to the database.

 public class ApplicationSignInManager : SignInManager<ApplicationUser, string> { public override async Task<System.Security.Claims.ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user) { var identity = await base.CreateUserIdentityAsync(user); identity.AddClaim(new System.Security.Claims.Claim("IsBirthday", user.DOB.GetShortDateString() == DateTime.Now.GetShortDateString())); return identity; } // ... EXCLUDING OTHER STUFF LIKE CONSTRUCTOR AND OWIN FACTORY METHODS ... } 
+11
source

Source: https://habr.com/ru/post/979293/


All Articles