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

     2023-02-19     73

关键词:

【中文标题】在 ASP.NET Core 中使用多个身份验证方案【英文标题】:Using multiple authentication schemes in 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  
        );     

【问题讨论】:

目前没有回复。没有人知道如何使用多重身份验证? 【参考方案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();
        

【讨论】:

如何在 ASP.NET Core 2.0 中设置多个身份验证方案?

...】:我正在尝试将我的身份验证内容迁移到Core2.0,但在使用我自己的身份验证方案时遇到了问题。我在启动时的服务设置如下所示:varauthenticationBuilder=services.AddAuthen 查看详情

尝试对 ASP.NET Core 3.1 使用多个身份验证方案时出现异常

】尝试对ASP.NETCore3.1使用多个身份验证方案时出现异常【英文标题】:ExceptionswhentryingtousemultipleauthenticationschemeswithASP.NETCore3.1【发布时间】:2021-07-0723:22:16【问题描述】:我正在尝试使用ASP.NETCore3.1添加多个身份验证方案,并在... 查看详情

如何在 ASP.NET Core 3.1 中启用多重身份验证?

...020-08-3101:32:49【问题描述】:在我的aspnetcore3.1项目中,我使用jwt进行身份验证。问题是我也在使用azure客户端来获取vm大小名称列表,并且它也在使用Bearer令牌。现在测试我使用azure中的AllowAnonymo 查看详情

检查用户是不是在 ASP.NET Core 中使用基于令牌的身份验证登录

】检查用户是不是在ASP.NETCore中使用基于令牌的身份验证登录【英文标题】:CheckifuserisloggedinwithTokenBasedAuthenticationinASP.NETCore检查用户是否在ASP.NETCore中使用基于令牌的身份验证登录【发布时间】:2017-05-1000:18:33【问题描述】:我... 查看详情

在 Blazor 中同时使用 ASP.Net Core Identity 和 Azure 身份验证

】在Blazor中同时使用ASP.NetCoreIdentity和Azure身份验证【英文标题】:UsingASP.NetCoreIdentityandAzureauthenticationtogetherinBlazor【发布时间】:2021-05-2807:25:25【问题描述】:我有一个服务器端Blazor应用程序,它已成功与Azure集成以进行身份​... 查看详情

ASP.NET Core 中的基本身份验证

...0916:06:02【问题描述】:问题如何在ASP.NETCoreWeb应用程序中使用自定义成员身份实现基本身份验证?备注在MVC5中,我使用了article中的说明,这需要在WebConfig中添加一个模块。我仍在IIS上部署我的新MVCCore应用程序,但这种方法似乎... 查看详情

如何使用 Microsoft 身份平台身份验证在 ASP.NET Core Web 应用程序中获取 JWT 令牌?

】如何使用Microsoft身份平台身份验证在ASP.NETCoreWeb应用程序中获取JWT令牌?【英文标题】:HowdoIgetholdofaJWTtokeninanASP.NETCoreWebAppusingMicrosoftidentityplatformauthentication?【发布时间】:2021-12-2404:30:55【问题描述】:我已经使用VisualStudio2022... 查看详情

如何在 ASP.NET Core 2.0 中根据路由配置服务身份验证

...2:06:40【问题描述】:在ASP.NETCore1.x中,我可以在Configure中使用身份验证方法,但现在在ASP.NETCore2.0中,我必须在ConfigureServices并且无法 查看详情

如何在 ASP.NET Core 3 上同时使用 Azure AD 身份验证和身份?

】如何在ASP.NETCore3上同时使用AzureAD身份验证和身份?【英文标题】:HowtousebothAzureADauthenticationandIdentityonASP.NETCore3?【发布时间】:2020-05-2814:49:45【问题描述】:Web应用程序应允许拥有AD帐户的内部员工使用AzureAD身份验证在应用程... 查看详情

Cookie 身份验证 ASP.NET Core

...间】:2017-03-3012:57:27【问题描述】:我可以在ITicketStore中使用MemoryCache来存储AuthenticationTicket吗?背景:我的网络应用正在使用Cookie身份验证:app.UseCookieAuthentication(newCookieAuthenticati 查看详情

如何在 ASP.NET Core Identity 中结合外部登录和双重身份验证?

...ty?【发布时间】:2020-04-0109:22:41【问题描述】:我正在为使用ASP.NETCore3.1构建的简单Web应用程序实现身份验证和授权。到目前为止,ASP.NETCoreIdentit 查看详情

在 ASP.NET Core Identity 中使用 Authorize 属性检查多个策略之一

】在ASP.NETCoreIdentity中使用Authorize属性检查多个策略之一【英文标题】:CheckingforoneofmultiplepolicieswithAuthorizeattributeinASP.NETCoreIdentity【发布时间】:2017-11-0918:51:37【问题描述】:我已经在ASP.NETCore应用程序中设置了标准身份验证系统... 查看详情

在 ASP .NET Core Web API 中使用没有身份的 cookie 身份验证时如何在登录时刷新 CSRF 令牌

】在ASP.NETCoreWebAPI中使用没有身份的cookie身份验证时如何在登录时刷新CSRF令牌【英文标题】:HowtorefreshCSRFtokenonloginwhenusingcookieauthenticationwithoutidentityinASP.NETCoreWebAPI【发布时间】:2020-09-1018:55:52【问题描述】:我有一个ASP.NETCore3.1... 查看详情

ASP.NET Core Web API 身份验证

...努力解决如何在我的网络服务中设置身份验证。该服务是使用ASP.NETCoreWebapi构建的。我的所有客户端(WPF应用程序)都应使用相同的凭据来调用Web服务操作。经过一番研究,我提出了基本身份验证-在HTTP请求的标头中发送用户名... 查看详情

在 Asp.Net Core 2 中使用 JwtBearer 身份验证时返回 401 而不是重定向到登录页面

】在Asp.NetCore2中使用JwtBearer身份验证时返回401而不是重定向到登录页面【英文标题】:Return401insteadofredirecttologinpagewhenusingJwtBearerauthenticationinAsp.NetCore2【发布时间】:2018-07-2009:15:00【问题描述】:在启动时我有services.AddAuthenticatio... 查看详情

asp.Net core 2.2中的多种身份验证方法

...:2019-06-1302:57:57【问题描述】:有没有办法在.net核心中使用JWT不记名身份验证和自定义身份验证方法?我希望所有操作都默认为JWT,除了在少数情况下我想使用自定义身份验证标头。【问题讨论】:这个问题似乎很简单。你看... 查看详情

ASP.Net-Core 中的自定义身份验证

...个需要与现有用户数据库集成的网络应用程序。我仍然想使用[Authorize]属性,但我不想使用Identity框架。如果我确实想使用Identity框架,我会在startup.cs文件中添加类似的内容:services.AddIdenti 查看详情

如何在asp.net core中添加多个身份和多个角色

】如何在asp.netcore中添加多个身份和多个角色【英文标题】:Howtoaddmultipleidentityandmultipleroleinasp.netcore【发布时间】:2021-01-2920:31:40【问题描述】:我是asp.net核心的新手。所以这可能不是一个好问题。我有3个课程:Admin、Doctor、Pa... 查看详情