在身份 3 中创建声明身份
Create claims identity in Identity 3
Visual Studio 2015 脚手架使用 UserManager<TUser>
不能用来创建 ClaimsIdentity
。有没有人有关于如何做到这一点的工作示例?
VS2015 脚手架抛出错误:
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;
}
N.B.: 我已经向 ApplicationUser
添加了与 IdentyUser
.
不冲突的属性
UserManager 在MVC6 版本中发生了变化。您将需要修改您的代码...
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) {
var authenticationType = "Put authentication type Here";
var userIdentity = new ClaimsIdentity(await manager.GetClaimsAsync(this), authenticationType);
// Add custom user claims here
return userIdentity;
}
.net 核心
答案已更改,根据 here and here,两位作者均声明使用 UserClaimsPrincipalFactory<ApplicationUser>
,这是核心 2.2 的默认实现。第一篇文章说你要找的方法搬家了。但是,如前所述,您必须像这样在服务中注册 UserClaimsPrincipalFactory
的实现,下面是一个示例 class 实现。请注意我们必须注册 MyUserClaimsPrincipalFactory
以便我们的服务集合知道在哪里可以找到它。这意味着在 SignInManager<ApplicationUser>
的构造函数中它也引用 IUserClaimsPrincipalFactory<ApplicationUser>
但服务会解析它:
services
.AddIdentity<ApplicationUser, ApplicationRole>()
.AddClaimsPrincipalFactory<MyUserClaimsPrincipalFactory>() // <======== HERE
services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, MyUserClaimsPrincipalFactory>();
下面是class:
public class MyUserClaimsPrincipalFactory : UserClaimsPrincipalFactory<ApplicationUser>
{
public MyUserClaimsPrincipalFactory(UserManager<ApplicationUser> userManager,
IOptions<IdentityOptions> optionsAccessor)
: base(userManager, optionsAccessor)
{
}
protected override async Task<ClaimsIdentity> GenerateClaimsAsync(ApplicationUser user)
{
var identity = await base.GenerateClaimsAsync(user);
identity.AddClaim(new Claim("ContactName", "John Smith"));
return identity;
}
}
Visual Studio 2015 脚手架使用 UserManager<TUser>
不能用来创建 ClaimsIdentity
。有没有人有关于如何做到这一点的工作示例?
VS2015 脚手架抛出错误:
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;
}
N.B.: 我已经向 ApplicationUser
添加了与 IdentyUser
.
UserManager 在MVC6 版本中发生了变化。您将需要修改您的代码...
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) {
var authenticationType = "Put authentication type Here";
var userIdentity = new ClaimsIdentity(await manager.GetClaimsAsync(this), authenticationType);
// Add custom user claims here
return userIdentity;
}
.net 核心
答案已更改,根据 here and here,两位作者均声明使用 UserClaimsPrincipalFactory<ApplicationUser>
,这是核心 2.2 的默认实现。第一篇文章说你要找的方法搬家了。但是,如前所述,您必须像这样在服务中注册 UserClaimsPrincipalFactory
的实现,下面是一个示例 class 实现。请注意我们必须注册 MyUserClaimsPrincipalFactory
以便我们的服务集合知道在哪里可以找到它。这意味着在 SignInManager<ApplicationUser>
的构造函数中它也引用 IUserClaimsPrincipalFactory<ApplicationUser>
但服务会解析它:
services
.AddIdentity<ApplicationUser, ApplicationRole>()
.AddClaimsPrincipalFactory<MyUserClaimsPrincipalFactory>() // <======== HERE
services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, MyUserClaimsPrincipalFactory>();
下面是class:
public class MyUserClaimsPrincipalFactory : UserClaimsPrincipalFactory<ApplicationUser>
{
public MyUserClaimsPrincipalFactory(UserManager<ApplicationUser> userManager,
IOptions<IdentityOptions> optionsAccessor)
: base(userManager, optionsAccessor)
{
}
protected override async Task<ClaimsIdentity> GenerateClaimsAsync(ApplicationUser user)
{
var identity = await base.GenerateClaimsAsync(user);
identity.AddClaim(new Claim("ContactName", "John Smith"));
return identity;
}
}