Razor 在标识3中创建声明标识

Razor 在标识3中创建声明标识,razor,entity-framework-6,asp.net-core,asp.net-identity-3,Razor,Entity Framework 6,Asp.net Core,Asp.net Identity 3,Visual Studio 2015脚手架使用了用户管理器,它不能用于创建索赔实体。有没有人有关于如何做到这一点的工作实例 VS2015脚手架抛出错误: public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) { // Note the authenticationType must match the one //

Visual Studio 2015脚手架使用了
用户管理器
,它不能用于创建
索赔实体
。有没有人有关于如何做到这一点的工作实例

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;
}
公共异步任务GenerateUserIdentityAsync(UserManager)
{
//注意,authenticationType必须与
//在CookieAuthenticationOptions.AuthenticationType中定义
var userIdentity=wait manager.CreateIdentityAsync(这是DefaultAuthenticationTypes.ApplicationOkie);
//在此处添加自定义用户声明
返回用户身份;
}

N.B.:我在
ApplicationUser
中添加了与
identityuser
不冲突的属性。MVC6版本中的UserManager已更改。您需要修改您的代码

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;
}
公共异步任务GenerateUserIdentityAsync(UserManager){
var authenticationType=“将身份验证类型放在此处”;
var userIdentity=newclaimsidentity(wait manager.GetClaimsAsync(this),authenticationType);
//在此处添加自定义用户声明
返回用户身份;
}

.net内核

答案已经改变,根据和,两位作者都声明使用
UserClaimsPrincipalFactory
,这是核心2.2的默认实现。第一篇文章说你正在寻找的方法已经改变了。但是,如上所述,您必须在类似so的服务中注册
UserClaimsPrincipalFactory
的实现,下面是一个示例类实现。请注意,我们必须注册
MyUserClaimsPrincipalFactory
,以便我们的服务集合知道在哪里可以找到它。这意味着在
SignInManager
的构造函数中,它也指的是
IUserClaimsPrincipalFactory
,但服务将解决它:

services
.AddIdentity<ApplicationUser, ApplicationRole>()              
.AddClaimsPrincipalFactory<MyUserClaimsPrincipalFactory>() // <======== HERE

services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, MyUserClaimsPrincipalFactory>();
服务
.AddIdentity()

.AddClaimsPrincipalFactory()//脚手架会抛出什么错误?在哪里?您发布的方法显示,它返回的
ClaimsIdentity
UserManager不包含CreateIdentityAsync或DefaultAuthenticationTypes的定义,因此无法复制:VS2015使用MVC 6模板创建新的ASP.NET web项目。在ApplicationUser.cs中的模型下,添加对System.Security.Claims和Micosoft.AspNet.Identity的引用,并将上述代码插入ApplicationUser类。请参阅上面评论中描述的错误。您所描述的将在MVC5而不是MVC6/core中工作。现在检查以查看它在新版本中的变化。请在将来使用正确的标记!当您的问题与ASP.NET核心相关时,请使用“ASP.NET核心”标记!不是“asp.net”和“core”,两者都与您的问题完全无关。在添加AuthenticationType之前,请阅读标签说明:我从公共演示项目Altostatus学习如何将DAL/DTO类添加到基于ASPNETCORE的新项目中,并注意到它们有两种异步方法,一种只使用
UserManager
,另一种使用
UserManager,字符串身份验证类型
两者都需要吗?正如我在你的文章中看到的,你只是硬编码了类型消息。这取决于你的需要。如果不需要多个类型,并且不太可能更改,则不需要。否则,请执行适用于您的代码的操作。如果我有一个已经设置了用户身份验证的MS SQL数据库,我是否在其中放置了任何内容,或者它不应该只是aspnetcore EF上的项目模板中的通用内容?表为aspnetroles、aspnetuserclaims、aspnetuserlogins、aspnetroles和aspnetusers。然后使用创建项目时提供的默认模板。当使用多个身份验证提供程序时,已经有一些示例可用。
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;
    }
}