Sam*_*Sam 11 c# asp.net asp.net-mvc owin asp.net-identity
任何人都可以解释为什么ApplicationUser该类创建以下帮助函数?
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User, int> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
Run Code Online (Sandbox Code Playgroud)
我能找到它的唯一地方是在Startup.Auth.cs文件中,作为函数的regenerateIdentity回调参数 SecurityStampValidator.OnValidateEntity:
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, User, int>(
validateInterval: TimeSpan.FromSeconds(15),
regenerateIdentityCallback: (manager, user) => user.GenerateUserIdentityAsync(manager),
getUserIdCallback: (id) => id.GetUserId<int>())
Run Code Online (Sandbox Code Playgroud)
正如你从助手那里看到的那样,它只是转身并打电话manager.CreatedIdentityAsync.有没有理由他们ApplicationUser用帮助方法"污染"这个类而不是设置OnValidateEntity如下?
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, User, int>(
validateInterval: TimeSpan.FromSeconds(15),
regenerateIdentityCallback: (manager, user) => manager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie),
getUserIdCallback: (id) => id.GetUserId<int>())
Run Code Online (Sandbox Code Playgroud)
*编辑清晰简洁
通过将Identity Generation方法抽象到用户类中,我们可以获得可扩展性.
想象一下,您的应用程序具有多种不同的用户类型,每种用户都可以实现自己的再生逻辑,而无需使用单独的身份验证类型.获取IdentityUser基类的ApplicationUser子类中的helper方法.
public class ApplicationUser : IdentityUser
{
public string NickName {get; set; }
public DateTime BirthDay {get; set;}
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
Run Code Online (Sandbox Code Playgroud)
我们现在可以将我们的声明分成不同的用户类,而无需修改OWIN身份验证管道,或者仅通过继承基本IdentityUser为每种类型创建新的CookieAuthenticationProvider.
tldr;
它将身份重新生成职责推送到正在重新生成的用户类.类似于工厂方法模式.
| 归档时间: |
|
| 查看次数: |
5367 次 |
| 最近记录: |