【问题标题】:ASP NET 5 - Custom Authentication scheme handler is ignored when using IdentityASP NET 5 - 使用身份时忽略自定义身份验证方案处理程序
【发布时间】:2021-02-27 17:14:53
【问题描述】:

自定义身份验证方案和处理程序将被忽略。 当控制器方法由[Authorize] 注释时,即使将其设置为默认身份验证方案,我的方案也不会受到挑战。相反,发送 302 状态并重定向到 /Accounts/Login...。 不仅这个 URL 甚至不存在于我的应用程序中(我在客户端使用 Blazor WASM),而且我的应用程序中根本没有任何代码会导致这种行为。 当我直接在属性中指定它时调用我的身份验证处理程序:[Authorize(AuthenticationScheme = CustomAuthHandler.SchemeName)]

为什么默认身份验证方案被忽略,而是运行代码中根本没有指定的东西? 如果AddIdentity() 配置了这种意外行为,它记录在哪里以及如何重新配置​​? 如何让框架不忽略我的处理程序,注册为默认?

在我切换到 .NET 5 并开始使用 Identity 框架之前,一切正常。

这是我的ConfigureServices

services.AddDbContext<ApplicationDbContext>(options =>
        {
            options.UseMySql(Configuration.GetConnectionString("DefaultConnection"), ServerVersion.FromString("10.5.6-mariadb"));
        });

services.AddIdentity<ApplicationUser, IdentityRole>(
        options =>
            {
                options.User.RequireUniqueEmail = true;
            }    
        )
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddClaimsPrincipalFactory<ApplicationUserClaimsFactory>();

services.AddControllersWithViews();
services.AddRazorPages();

services.AddAuthentication(
     options =>
     {
         options.DefaultScheme = CustomAuthHandler.SchemeName;
     }
)
.AddScheme<AuthenticationSchemeOptions, CustomAuthHandler>(CustomAuthHandler.SchemeName, x => {  });

【问题讨论】:

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


    【解决方案1】:

    好的,AddIdentity() 方法实际上导致了问题。 解决方案是将其替换为AddIdentityCore(),这是一种更轻量级的方法,并且在后台执行的操作更少。然后必须手动添加缺少的服务,例如AddRoles(),如文档中所述。

    【讨论】:

      猜你喜欢
      • 2018-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多