带范围的Asp.net核心2.1 OpenIdConnectOptions不起作用

编程入门 行业动态 更新时间:2024-10-24 07:30:17
本文介绍了带范围的Asp核心2.1 OpenIdConnectOptions不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

请告诉我为什么我不能向OpenIdConnectOptions添加任何作用域?它不适用于ASP.NET Core MVC客户端,但适用于js客户端,可以正常工作!

Please tell me why I can not add any scope to OpenIdConnectOptions? It doesn't work with an ASP.NET Core MVC client, but with a js client it works fine!

我的代码...

IdentityServer4 客户端注册

public static IEnumerable<Client> GetClients() { return new List<Client> { new Client { ClientId = "web", ClientName = "Web Client", AllowedGrantTypes = GrantTypes.Implicit, AllowAccessTokensViaBrowser = true, AlwaysIncludeUserClaimsInIdToken = true, RedirectUris = {"localhost:5002/signin-oidc"}, PostLogoutRedirectUris = {"localhost:5002/signout-callback-oidc"}, AllowedCorsOrigins = {"localhost:5002"}, AllowedScopes = { IdentityServerConstants.StandardScopes.OpenId, IdentityServerConstants.StandardScopes.Profile, "api1" }, AccessTokenLifetime = 300, IdentityTokenLifetime = 3600, AllowOfflineAccess = true, } }; }

接下来,我将代码添加到mvc客户端以使用授权.

Next, I add code to the mvc client to use authorization.

Mvc客户端

JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); services.AddAuthorization(options => options.AddPolicy("AdminsOnly", policyUser => { policyUser.RequireClaim("role", "admin"); })); services.AddAuthentication(options => { options.DefaultScheme = "Cookies"; options.DefaultChallengeScheme = "oidc"; }) .AddCookie("Cookies") .AddOpenIdConnect("oidc", options => { options.SignInScheme = "Cookies"; options.Authority = "localhost:5000"; options.RequireHttpsMetadata = false; options.GetClaimsFromUserInfoEndpoint = true; options.Scope.Add("api1"); options.ClientId = "web"; options.SaveTokens = true; });

当我尝试切换到标有[authorize]属性的操作时,出现错误

When I try to switch to an action marked with the [authorize] attribute, I get an error

对不起,出现错误:invalid_scope.

Sorry, there was an error : invalid_scope.

如果我删除行options.Scope.Add("api 1");,则身份验证有效.但是在这种情况下,我无法指定角色以及更多...

If I delete the line options.Scope.Add("api 1"); then the authentication works. But in this case I can not specify roles and more...

可以在此处下载该项目

The project can be downloaded here

推荐答案

将此行添加到MVC客户端的AddOpenIdConnect选项中,以请求身份令牌和访问令牌:

Add this line to your MVC client's AddOpenIdConnect options to request an identity token and access token:

options.ResponseType = "id_token token";

您的JS客户端要求一个身份令牌和一个访问令牌,而MVC客户端仅要求一个身份令牌和资源身份令牌中不允许使用范围.请参见 docs.identityserver.io/en/release/endpoints/authorize .html :

Your JS client is asking for an identity token and an access token whereas the MVC client is only asking for an identity token and resource scopes are not allowed in identity tokens. See docs.identityserver.io/en/release/endpoints/authorize.html:

id_token请求一个身份令牌(仅允许身份范围)

id_token requests an identity token (only identity scopes are allowed)

令牌请求访问令牌(仅允许资源作用域)

token requests an access token (only resource scopes are allowed)

更多推荐

带范围的Asp.net核心2.1 OpenIdConnectOptions不起作用

本文发布于:2023-11-16 10:11:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1603350.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   核心   net   Asp   OpenIdConnectOptions

发布评论

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

>www.elefans.com

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