提供非默认架构时如何禁用默认身份验证方案

编程入门 行业动态 更新时间:2024-10-27 12:25:39
本文介绍了提供非默认架构时如何禁用默认身份验证方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的应用程序中有两种身份验证方案

I have two authentication schemes in my app

services.AddAuthentication("default") .AddJwtBearer("default", options => { // some options }) .AddJwtBearer("non-default", options => { // some other options });

该想法是对大多数控制器使用默认值,并且当需要非默认值时,使用 [Authorize(AuthenticationSchemes ="non-default")] .问题是,即使设置了非默认模式,也始终会调用默认模式.它运行并失败,然后正确的架构运行并成功.但这会导致日志中充满无法验证令牌"消息.有没有办法禁用默认架构?

The idea is to use the default for most of the controllers, and when the non-default is needed, to explicitly mention the needed schema with [Authorize(AuthenticationSchemes = "non-default")]. The problem is, the default schema is always being called, even when the non-default is set. It runs and fails, and after that the correct schema runs and succeeds. But this results in the log full of "Failed to validate the token" messages. Is there a way to disable the default schema?

我使用的是net core 2.2,但考虑升级到3.1.

I use net core 2.2, but considering to move to 3.1.

推荐答案

我发现解决方案不是提供默认的身份验证方法,而是提供默认的授权策略.

I found the solution in providing not the default authentication method, but rather the default authorization policy.

services.AddAuthentication() .AddJwtBearer("defaultScheme", options => { // some options }) .AddJwtBearer("nonDefaultScheme", options => { // some other options }); services.AddAuthorization(opts => { opts.DefaultPolicy = new AuthorizationPolicyBuilder() .AddAuthenticationSchemes("defaultScheme") .RequireAuthenticatedUser() .Build(); opts.AddPolicy("non-default", policy => policy .AddAuthenticationSchemes("nonDefaultScheme") .RequireAuthenticatedUser()); });

此后, [Authorize] 和 [Authorize("non-default")] 都正常工作,仅调用一种身份验证方案.

After this both [Authorize] and [Authorize("non-default")] work normally, only calling one of the authentication schemes.

更多推荐

提供非默认架构时如何禁用默认身份验证方案

本文发布于:2023-10-28 20:02:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1537634.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:架构   身份验证   方案

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!