【问题标题】:Custom ASP.NET Core Cookie Authentication自定义 ASP.NET Core Cookie 身份验证
【发布时间】:2017-02-15 15:37:49
【问题描述】:

过去几天我一直在研究 ASP.NET Core 中我的服务的身份验证。我的应用程序有一个简单的身份验证令牌系统。预计该请求上有一个 cookie,我获取该 cookie 并向我的身份验证服务器发出请求。身份验证服务器将用户的权利归还给我。如果 cookie 不存在,或者身份验证请求返回失败,则应用应输出 401。如果成功,它将转到管道的下一部分并检查权利的授权。

我按照预期设置了我的身份验证中间件 - 从 AuthenticationHandler、AuthenticationMiddleware 等继承。我的自定义身份验证处理程序从 Authenticationhandler 继承并覆盖 HandleAuthenticateAsync()。此方法使用用户提供的 cookie 来获取用户数据,创建我的 ClaimsPrincipal,并返回 AuthenticateResult.Success 或 AuthenticateResult.Fail。

当 AuthenticationResult.Fail 返回时,我认为应用程序会退出,但我的应用程序仍会转到管道的下一部分 (app.UseMvc()),当时我认为它会返回 401 错误。

我的 Startup.cs 如下所示。

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();
        Configuration = builder.Build();
    }

    public IConfigurationRoot Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddAuthentication();
        services.AddMvc();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        app.UseCustomAuthentication(new CustomAuthenticationOptions()
        {
            AutomaticChallenge = true,
            AutomaticAuthenticate = true
        });

        app.UseMvc();
    }
}

这将使身份验证失败,我会在输出中看到它,但 UseMvc 仍将运行。直到我对服务执行此操作后,它才会退出,但出现授权错误,而不是应该标记的身份验证错误。

        services.AddMvc(config =>
        {
            var policy = new AuthorizationPolicyBuilder()
                             .RequireAuthenticatedUser()
                             .Build();
            config.Filters.Add(new AuthorizeFilter(policy));
        });

这是应该如何设置的吗?身份验证失败时,管道不应该关闭吗?

【问题讨论】:

    标签: asp.net .net asp.net-web-api asp.net-core .net-core


    【解决方案1】:

    身份验证失败时,管道不应该关闭吗?

    假设您有另一个身份验证中间件:

        app.UseCustomAuthentication(new CustomAuthenticationOptions()
        {
            AutomaticChallenge = true,
            AutomaticAuthenticate = true
        });
        app.UseOtherAuthentication(new OtherAuthenticationOptions()
        {
            AutomaticChallenge = true,
            AutomaticAuthenticate = true
        });
    

    如果管道在第一次身份验证失败时结束,则其他身份验证中间件永远不会运行。它的可扩展性会降低。

    另外一点,假设您希望使用AllowAnonymous 属性允许对匿名请求执行某些操作。你怎么允许?

    即使认证中间件失败,如果不调用HttpContext.Authentication.ChallengeAsync() 或使用Authorize 属性,服务器也不会响应401、403 或302。

    据我所知,这是预期行为,内置 cookie 身份验证的行为相同。如果你想首先强制认证用户,你需要全局添加它(就像你做的那样)或在控制器顶部使用Authorize 属性。

    【讨论】:

      猜你喜欢
      • 2018-02-24
      • 2018-07-19
      • 1970-01-01
      • 2017-03-30
      • 2016-07-05
      • 2018-03-15
      • 1970-01-01
      相关资源
      最近更新 更多