身份的方案名称是什么?

时间:2019-02-24 03:44:41

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

假设我使用以下内容:

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

设置的认证方案名称是什么?我在任何文档中都找不到。我尝试寻找名称为IdentityAuthenticationDefaultsIdentityDefaults的课程,但一无所获。我尝试过“ Cookies”,但未设置为此。该应用程序运行良好,因此肯定设置了一些方案名称。

2 个答案:

答案 0 :(得分:5)

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参考文档的链接:

答案 1 :(得分:0)

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

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

然后就可以这样使用了。

[Authorize(AuthenticationSchemes = SharedConstants.IdentityApplicationScheme)]
相关问题