小编Sla*_*vvy的帖子

Asp.net core 2.0+ - 多种认证方案(Cookie / Bearer)

我一直在努力使多个身份验证方案在 Asp.net 核心 2.1 中正常工作。

我使用带有隐式流和 OpenIdConnect 作为协议的 Identity Server。

当仅使用其中一种方案(例如 Cookie 或 Bearer)授权操作或控制器时,该功能可以正常工作。

例子:

  [Authorize(AuthenticationSchemes = "Cookies")]
  [Route("Cookies")]
  public class BearerAndCookiesController : Controller {
Run Code Online (Sandbox Code Playgroud)

但是,如果我在 Authorize 属性上指定了两种方案,那么它会部分失败。Bearer 正常工作,但是当我尝试在浏览器中查看页面时,它尝试重定向到本地登录页面 ( http://localhost/Account/Login )。

当我检查 Identity Server 的调试日志时,没有返回任何内容,这是有道理的,因为它没有尝试联系管理局。但是,当我查看测试 MVC 站点的调试日志时,Bearer 和 Cookie 方案都受到挑战:

Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 GET http://localhost:5002/cookies  
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Route matched with {action = "Get", controller = "BearerAndCookies"}. Executing action MvcClient.Controllers.BearerAndCookiesController.Get (MvcClient)
Microsoft.AspNetCore.Authorization.DefaultAuthorizationService:Information: Authorization failed.
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Authorization failed for the request at filter 'Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter'.
Microsoft.AspNetCore.Mvc.ChallengeResult:Information: Executing ChallengeResult with authentication schemes (Bearer, …
Run Code Online (Sandbox Code Playgroud)

cookies authorization bearer-token asp.net-core identityserver4

3
推荐指数
1
解决办法
2305
查看次数