【问题标题】:Create claims identity in Identity 3在身份 3 中创建声明身份
【发布时间】:2016-07-31 10:19:50
【问题描述】:

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;
}

注意:我已向ApplicationUser 添加了与IdentyUser 不冲突的属性。

【问题讨论】:

  • 脚手架会抛出什么错误,在哪里?您发布的方法显示它正在返回 ClaimsIdentity
  • UserManager 不包含 CreateIdentityAsync 或 DefaultAuthenticationTypes 的定义
  • 重现:VS2015 使用 MVC 6 模板创建一个新的 ASP.NET Web 项目。在 ApplicationUser.cs 中的模型下,添加对 System.Security.Claims 和 Micosoft.AspNet.Identity 的引用,并将上面的代码插入到 ApplicationUser 类中。请参阅上面评论中描述的错误。
  • 您所描述的内容适用于 mvc 5 而不是 mvc 6/core。现在查看它在新版本中的变化
  • 以后请使用正确的标签!当您的问题与 ASP.NET Core 相关时,请使用“asp.net-core”标签!不是“asp.net”和“core”,两者都与您的问题完全无关。添加前请阅读标签说明

标签: razor entity-framework-6 asp.net-core asp.net-identity-3


【解决方案1】:

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;
}

【讨论】:

  • 我正在学习如何将 DAL/DTO 类添加到基于公共演示项目 Altostratus 的 ASPNETCORE 的新项目中,并注意到他们有两个异步方法,一个只需要 UserManager&lt;ApplicationUser&gt; manager 和另一个需要UserManager&lt;ApplicationUser&gt; manager, string authenticationType 他们都需要吗?正如我在您的文章中看到的,您只需对类型消息进行硬编码。
  • 这取决于您的需求。如果不需要多种类型并且不太可能改变,那么不需要。否则做对你的代码有用的事情。
  • 如果我有一个已经设置了用户身份验证的 MS SQL 数据库,我是否应该在其中放置任何内容,或者它不应该只是 aspnetcore EF 上的项目模板中的通用内容?表是 aspnetroles、aspnetuserclaims、aspnetuserlogins、aspnetroles 和 aspnetusers。
  • 然后使用创建项目时提供的默认模板。当使用多个身份验证提供程序时,已经有一些示例可供使用。
【解决方案2】:

.net 核心

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

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

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

这是下面的类:

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;
    }
}

【讨论】:

    猜你喜欢
    • 2021-08-26
    • 2016-03-01
    • 2018-05-09
    • 2012-02-25
    • 1970-01-01
    • 2016-02-19
    • 1970-01-01
    • 2017-12-22
    • 2017-08-03
    相关资源
    最近更新 更多