【问题标题】:What is the scheme name for identity?身份的方案名称是什么?
【发布时间】:2019-07-17 19:31:46
【问题描述】:

假设我使用以下内容:

services.AddIdentity<User, UserRole>()
        .AddEntityFrameworkStores<AppDbContext>();

正在设置的身份验证方案名称是什么?我在任何文档中都没有找到这个。我尝试寻找名称为IdentityAuthenticationDefaultsIdentityDefaults 的课程,但一无所获。我已经尝试过“Cookies”,但它没有设置为这个。该应用程序运行良好,因此肯定设置了一些方案名称。

【问题讨论】:

    标签: c# asp.net-core asp.net-identity asp.net-core-identity .net-core-authorization


    【解决方案1】:

    IdentityConstants 是您在此处寻找的课程。这是您特定问题的相关部分(已删除 xmldocs):

    public class IdentityConstants
    {
        private static readonly string CookiePrefix = "Identity";
    
        public static readonly string ApplicationScheme = CookiePrefix + ".Application";
    
        ...
    }
    

    IdentityConstants.ApplicationScheme 用作DefaultAuthenticateScheme - 值本身最终成为Identity.Application

    方案建立here:

    services.AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme = IdentityConstants.ApplicationScheme;
        options.DefaultChallengeScheme = IdentityConstants.ApplicationScheme;
        options.DefaultSignInScheme = IdentityConstants.ExternalScheme;
    })
    

    以下是 API 参考文档的链接:

    【讨论】:

    • 谢谢,它解决了这个问题。然而,由于这不是一个常数,关于如何使用它Authorize 没有硬编码 Identity.Application 的任何想法?我想创建一个自定义身份验证方案将是一个很长的路要走。
    • 一个自定义策略应该适合这种情况 - 请参阅 docs 了解其工作原理。
    【解决方案2】:

    IdentityConstants 下有静态字符串引用,但 AuthorizeAttribute 类或方法属性不能使用这些引用来设置 AuthenticationSchemes 属性,因为它必须是一个常量值。我创建了一个简单的共享常量类,其中包含解决此问题的必要条件,但希望 MS 提供一些 OOTB。

    public class SharedConstants
    {
        public const string IdentityApplicationScheme = "Identity.Application";
    }
    

    那你就可以这样使用了。

    [Authorize(AuthenticationSchemes = SharedConstants.IdentityApplicationScheme)]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-29
      • 2010-12-08
      • 2022-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-09
      • 2011-04-03
      相关资源
      最近更新 更多