I have some extra information that I need to add to user claims that are from other tables and classes.
Given the method generated by the ASP.NET MVC template with Identity in 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;
}
}
This does not have an EF Context available, and also, this method is generated at times when there is no context yet created in 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% with%, from CreateUserIdentityAsync
you can even have, taking into account the moment, 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 context-loaded claims?