【问题标题】:AuthenticationHandler ending request prematurelyAuthenticationHandler 过早结束请求
【发布时间】:2017-08-22 20:30:25
【问题描述】:

我正在尝试使用一些自定义逻辑制作一个 .net 核心 AuthenticationHandler。每当我向页面发出请求时,auth 处理程序中的所有内容都运行良好,但它返回 200 而没有实际执行我的最终控制器的代码。我把它提炼成这个简化的版本。

Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    services.AddAuthentication("Dummy")
        .AddScheme<AuthenticationSchemeOptions, DummyAuthHandler>("Dummy", null);
    ...

我的处理程序:

public class DummyAuthHandler : AuthenticationHandler<AuthenticationSchemeOptions>
{
    public DummyAuthHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, 
        ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : 
            base(options, logger, encoder, clock)
    {
    }

    protected override Task<AuthenticateResult> HandleAuthenticateAsync()
        => Task.FromResult(AuthenticateResult.Success(
               new AuthenticationTicket(new ClaimsPrincipal(), "Dummy")));

    protected override Task HandleChallengeAsync(AuthenticationProperties properties)  
        => Task.CompletedTask;
}

我想我错过了告诉框架继续处理请求所需的方法之一,而不是仅仅认为我的身份验证处理程序想要重定向页面。也许我什至需要在某处添加对 next() 的调用?

【问题讨论】:

    标签: asp.net-core


    【解决方案1】:

    感谢thisthis 破解它... ClaimsIdentity 返回 IsAuthenticated = False,这就是为什么我认为我首先需要 HandleChallengeAsync。下面是固定处理程序的样子:

    public class DummyAuthHandler : AuthenticationHandler<AuthenticationSchemeOptions>
    {
        public DummyAuthHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, 
            ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : 
                base(options, logger, encoder, clock)
        {
        }
    
        protected override Task<AuthenticateResult> HandleAuthenticateAsync()
            => Task.FromResult(AuthenticateResult.Success(
                   new AuthenticationTicket(new ClaimsPrincipal(
                       new ClaimsIdentity("abc")), "Dummy")));
    }
    

    【讨论】:

      猜你喜欢
      • 2013-03-13
      • 1970-01-01
      • 1970-01-01
      • 2017-09-28
      • 2014-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多