从jwt代币中获取索赔。
我正在尝试从我的ASP.NET Core 5 Web API中获取JWT令牌的索赔。我正在使用Azure,并已在我们的房客中注册了Azure广告中的应用程序。
该代码在授权
处理程序中。
当我使用 JWTSecurityTokenHandler
读取JWT令牌( context.request.request.headers [“授权”]
)时,我可以得到所有索赔,但是我的 contection.user。索赔
仍然是空的。
AUD
和 ISS
值显示为:
[aud, https://graph.microsoft.com]
[iss, https://sts.windows.net/[tenant-id]/]
这是我的 startup.configureservices()
方法:
services.AddAuthentication(sharedOptions =>
{
sharedOptions.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(option =>
{
option.Audience = audience;
option.Authority = authority;
option.TokenValidationParameters = new TokenValidationParameters
{
ClockSkew = TimeSpan.FromHours(1),
ValidateLifetime = true,
ValidateIssuer = true,
ValidIssuer = authority,
ValidateAudience = true,
ValidAudience = audience
};
});
我应该放在>中受众
和权限
?我已经测试了我从JWT令牌获得的值(上图)。
我还尝试了:
-
权威(发行者):
https://login.microsoftonline.com/ [tenant-id]/v2.0
-
受众:
app:// [client-id]
和所有组合。相同的结果
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最初,尝试使用
对于受众参数,您可以使用应用程序ID uri (api:// your_app_id)或 scope (
对于授权参数,您可以使用令牌发行的身份验证服务器的地址。 Please note that, issuer value differs depending on the
v1.0令牌 - >
https://sts.windows.net< Azure AD租户Guid&gt;/
v2.0令牌 - &gt;
https://login.microsoftonline.com< azure ad tenant guid&gt;/v2.0
To confirm and know more about the parameters, refer to the
请检查您是否包括
app.useauthorization
是否包括。确保按以下订单拨打中间件,如以下 ms doc :
如果问题持续存在,请尝试修改
startup.cs - &gt; configureservices()
方法如下所述-aspnet-core#设置-JWT-jwt-authentication and ostrorization“ rel =“ nofollow noreferrer”> blog 。您可以参考 下面的链接可以为您提供一些解决问题的指示:
sidentityServer4-user.claims是空的ASP.NET 5.0-stack Overflow
null-堆栈溢出
Initially, try to decode the token using jwt.ms and check what claims the token contains.
For Audience parameter, you can use the Application ID URI (api://your_app_id) or scope (https://graph.microsoft.com).
For Authority parameter, you can use the address of the token-issuing authentication server. Please note that, issuer value differs depending on the type of token you are generating (v1.0/v2.0).
For v1.0 token ->
https://sts.windows.net< Azure AD Tenant GUID>/
For v2.0 token ->
https://login.microsoftonline.com<Azure AD Tenant GUID>/v2.0
To confirm and know more about the parameters, refer to the blog by Jeffrey Fritz.
Please check whether you included
app.UseAuthorization
or not.Make sure to call the middleware in the order like below from this MS Doc:
If the issue persists, then try to modify the
Startup.cs -> ConfigureServices()
method as mentioned in this blog.You can refer to the links below that can give you some pointers to resolve the issue:
identityserver4 - User.Claims is empty ASP.NET 5.0 - Stack Overflow
.NET Core Web API HttpContext.User.Claimsare always null - Stack Overflow
我怀疑您在JWT标头中使用
nonce
字段的JWT类型错误,该字段在您自己的API中无法正确验证。要解决此问题,有一个公开API范围
选项。从我的几年前。然后,您可以像我的 API代码示例配置。
I suspect you are getting the wrong type of JWT, with a
nonce
field in the JWT header, that does not validate properly in your own APIs. To fix this there is anExpose an API scope
option.There are some visual details about this from step 3 of my blog post from a few years back. You can then configure the issuer and audience as in my API code example config.