c# - ASP.Net 核心 2 : Default Authentication Scheme ignored

标签 c# authentication asp.net-core asp.net-core-mvc

我正在尝试在 ASP.Net Core 2 中构建自定义 AuthenticationHandler。跟进类似 ASP.NET Core 2.0 authentication middleware 的主题和 Why is Asp.Net Core Authentication Scheme mandatory ,我已经创建了特定的类。注册过程如下:

services.AddAuthentication(
    options =>
    {
        options.DefaultScheme = Constants.NoOpSchema;
        options.DefaultAuthenticateScheme = Constants.NoOpSchema;
        options.DefaultChallengeScheme = Constants.NoOpSchema;
        options.DefaultSignInScheme = Constants.NoOpSchema;
        options.DefaultSignOutScheme = Constants.NoOpSchema; 
        options.DefaultForbidScheme = Constants.NoOpSchema;
    }
).AddScheme<CustomAuthOptions, CustomAuthHandler>(Constants.NoOpSchema, "Custom Auth", o => { });

一切正常,如果特定的 Controller 明确地设置了方案:

[Authorize(AuthenticationSchemes= Constants.NoOpSchema)]
[Route("api/[controller]")]
public class IndividualsController : Controller

但我不想设置架构,因为它应该动态添加。一旦我删除了 Scheme 属性,就像这样:

[Authorize]
[Route("api/[controller]")]
public class IndividualsController : Controller

它不再起作用了。

我希望设置 DefaultScheme 属性可以完成这项工作。有趣的是,我没有找到关于这个话题的任何具体讨论。 我在这里做错了什么还是我预期的结果有误?

编辑:感谢您的提问,这对我帮助很大。当 CustomAuthHandler 不存在时,我只使用身份验证中间件映射 DefaultScheme。因此,我必须始终添加 AuthenticationMiddleware。

Edit2:不幸的是,它仍然不起作用。为了加强一点我的问题:我像往常一样添加中间件:

app.UseAuthentication();
app.UseMvc();

现在我进入我的处理程序,它看起来像这样:

public class NoOpAuthHandler : AuthenticationHandler<NoOpAuthOptions>
{
    public const string NoOpSchema = "NoOp";

    public NoOpAuthHandler(IOptionsMonitor<NoOpAuthOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock)
    {
    }

    protected override Task<AuthenticateResult> HandleAuthenticateAsync() => Task.FromResult(AuthenticateResult.Success(new AuthenticationTicket(Context.User, NoOpSchema)));
}

但即使我总是返回成功,我也会收到 401。我认为我必须更深入地挖掘并以某种方式设置一些声明,但不幸的是,Microsoft 的处理程序很难分析,因为它们包含很多代码。

最佳答案

ASP.NET Core 2 答案

您必须设置与您的身份验证方案绑定(bind)的默认授权策略:

services.AddAuthorization(options => {
  options.DefaultPolicy = new AuthorizationPolicyBuilder()
    .AddAuthenticationSchemes(Constants.NoOpSchema)
    .RequireAuthenticatedUser()
    .Build();
});

ASP.NET Core 3 答案

在 ASP.NET Core 3 中,事情显然发生了一些变化,因此您需要创建一个扩展方法来添加您的身份验证处理程序:

public static class NoOpAuthExtensions
{
    public static AuthenticationBuilder AddNoOpAuth(this AuthenticationBuilder builder)
        => builder.AddNoOpAuth(NoOpAuthHandler.NoOpSchema, _ => { });
    public static AuthenticationBuilder AddNoOpAuth(this AuthenticationBuilder builder, Action<NoOpAuthOptions> configureOptions)
        => builder.AddNoOpAuth(NoOpAuthHandler.NoOpSchema, configureOptions);
    public static AuthenticationBuilder AddNoOpAuth(this AuthenticationBuilder builder, string authenticationScheme, Action<NoOpAuthOptions> configureOptions)
        => builder.AddNoOpAuth(authenticationScheme, null, configureOptions);
    public static AuthenticationBuilder AddNoOpAuth(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action<NoOpAuthOptions> configureOptions)
    {
        return builder.AddScheme<NoOpAuthOptions, NoOpAuthHandler>(authenticationScheme, displayName, configureOptions);
    }
}

并在您的 ConfigureServices 方法中使用它,如下所示:

services
  .AddAuthentication(NoOpAuthHandler.NoOpSchema)
  .AddNoOpAuth();

关于c# - ASP.Net 核心 2 : Default Authentication Scheme ignored,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49916216/

相关文章:

c# - MS Application Insights - 如何限制 Sql 依赖关系

c# - 计算linq中两个数组中相同元素的数量

java - JAVA 中的 cURL 等效项

node.js - 如何在 Node.js + MongoDB + PassportJS 应用程序中进行异步身份验证

c# - 捕获 ASP.NET Core MVC 中的异常

c# - 开发期间访问 Azure 存储时无法加载程序集 'Microsoft.Data.OData'

c# - datagridview中的单元格双击事件

authentication - 由于登录失败,SVN checkout 失败

c# - 仅向 DbContext 添加数据一次

c# - ASP.net Core中host和server的区别和联系