3
I have some extra information that I need to add to the user’s Claims that are from other tables and classes.
Considering the method generated by the ASP.NET MVC template with Identity in the class ApplicationUser:
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(
UserManager<ApplicationUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(
this, DefaultAuthenticationTypes.ApplicationCookie);
userIdentity.AddClaim(new Claim(ClaimsKey.ClaimA, "ClaimA"));
userIdentity.AddClaim(new Claim(ClaimsKey.ClaimB, "ClaimB"));
userIdentity.AddClaim(new Claim(ClaimsKey.ClaimC, "ClaimC"));
return userIdentity;
}
}
The one that does not have an EF Context available and that, also, this method is generated at times that there is no context yet created in the request, as in:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator
.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
TimeSpan.FromMinutes(30),
// aqui, principalmente ao efetuar login.
// tentei obter o contexto de
// HttpContext.Current.GetOwinContext...
// e obtive um erro por conta de HttpContext.Current
// que ainda estava null
(manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
In the CreateUserIdentityAsync, of ApplicationSignInManager may even have, taking into account the time, which is usually requested after user login:
public override Task<ClaimsIdentity> CreateUserIdentityAsync(Usuario user)
{
return user.GenerateUserIdentityAsync((AppUserManager)UserManager);
}
Taking these issues into consideration, what is the best time or a good way to add extra data to the Aims, loaded by context?
Favorite. I answer when I can.
– Leonel Sanches da Silva
Sorry for the delay.
– Leonel Sanches da Silva