【发布时间】:2021-11-14 12:27:05
【问题描述】:
我在 .NET Core 3.1 上,我正在尝试向 JWT 令牌添加自定义声明,但我无法这样做。如果您查看下面的代码,我可以使用这行代码读取自定义令牌
User.Claims.Where(x => x.Type == "role")
但是当我获取 JWT 令牌并将其放入 JWT.ms 时,令牌中不存在新的自定义声明。 .OnTokenValidated 是我尝试在 idp 返回之前将自定义声明添加到令牌的事件。
AddOpenIdConnect("test",o => {
o.SignInScheme = "Cookies";
o.SignOutScheme = "Cookies";
o.ClientId = "f";
o.ClientSecret = "0e";
o.Authority = "https://test.com";
o.ResponseType = OpenIdConnectResponseType.Code;
o.MetadataAddress = "https://test.com/.well-known/openid-configuration";
**o.Events.OnTokenValidated = async (ctx) =>
{
var claimsIdentity = ctx.Principal.Identity as ClaimsIdentity;
claimsIdentity.AddClaim(new Claim("role", "Admin"));
};**
o.SaveTokens = true;
o.GetClaimsFromUserInfoEndpoint = true;
【问题讨论】:
标签: c# .net-core asp.net-web-api jwt openid