在ASP.NET Core中使用多个身份验证方案

时间:2017-05-05 09:03:50

标签: asp.net-mvc authentication asp.net-core basic-authentication bearer-token

我使用ASP.NET Core开发了Web API,我需要能够为同一服务使用Basic和Bearer身份验证方案。 由于某种原因,它不起作用:它总是将呼叫视为持有者。 这是我的代码:

这是我在控制器中的属性:

[Authorize(ActiveAuthenticationSchemes = "Basic,Bearer")]
[ResponseCache(NoStore = true, Duration = 0, VaryByHeader = "Authorization")]

这是我的startup.cs:

这部分用于基本身份验证:

   app.UseBasicAuthentication(new BasicAuthenticationOptions
        {
            AutomaticAuthenticate = false,
            AutomaticChallenge = false,
            Realm = "test",
            Events = new BasicAuthenticationEvents
            {
                OnValidateCredentials = context =>
                {
                    if (svc.IsValidCredential(context.Username, context.Password))
                    {
                        var claims = new[]
                        {
                        new Claim(ClaimTypes.NameIdentifier, context.Username),
                        new Claim(ClaimTypes.Name, context.Username)
                        };

                        context.Ticket = new AuthenticationTicket(
                            new ClaimsPrincipal(
                                new ClaimsIdentity(claims, context.Options.AuthenticationScheme)),
                            new AuthenticationProperties(),
                            context.Options.AuthenticationScheme);
                    }

                    return Task.FromResult<object>(null);
                }
            }
        });

这段代码用于承载身份验证:

    app.UseAPIKeyAuthentication(new BearerApiKeyOptions
        {
            AuthenticationScheme = BearerApiKeySchema,
            AutomaticAuthenticate = false  
        });     

1 个答案:

答案 0 :(得分:3)

您可以查看this以获取官方Microsoft GitHub的参考资料。

我的用例略有不同,我需要Cookie和Windows身份验证的组合。您需要使用PolicyBuilder来强制执行“要求身份验证”。一部分。

在ConfigureServices方法上:

            // add additional authorisation for cookie
            services.AddAuthorization(options =>
            {
                options.AddPolicy("CookiePolicy", policy =>
                {
                    policy.AddAuthenticationSchemes("NTLM", "MyCookie"); // order does matter. The last scheme specified here WILL become the default Identity when accessed from User.Identity
                    policy.RequireAuthenticatedUser();
                });
            });

在配置方法:

            app.UseCookieAuthentication(new CookieAuthenticationOptions()
            {
                AuthenticationScheme = "MyCookie",
                LoginPath = new PathString("/Account/Login/"),
                AccessDeniedPath = new PathString("/Account/AccessDenied/"),
                AutomaticAuthenticate = false, // this will be handled by the authorisation policy
                AutomaticChallenge = false // this will be handled by the authorisation policy
            });

在控制器上:

        [Authorize("CookiePolicy")] // will check policy with the required authentication scheme (cookie in this case)
        public IActionResult AuthorisedPageCookie()
        {
            return View();
        }