【问题标题】:Using multiple authentication schemes in ASP.NET Core在 ASP.NET Core 中使用多个身份验证方案
【发布时间】:2017-10-03 16:31:39
【问题描述】:

我有使用 ASP.NET Core 开发的 Web API,我需要能够为同一服务使用基本和不记名身份验证方案。 由于某种原因,它不起作用:它始终将呼叫视为承载呼叫。 这是我的代码:

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

[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);
                }
            }
        });

还有这段用于Bearer认证的代码:

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

【问题讨论】:

  • 目前没有回复。没有人知道如何使用多重身份验证?

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


【解决方案1】:

您可以查看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();
        }

【讨论】:

    猜你喜欢
    • 2020-10-01
    • 2019-08-31
    • 2018-02-23
    • 2020-07-27
    • 2018-01-23
    • 1970-01-01
    • 2022-01-04
    • 2022-07-27
    • 2019-07-10
    相关资源
    最近更新 更多