获取简短声明类型名称 [英] Get short claim type name

查看:62
本文介绍了获取简短声明类型名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Asp.Net Core和ASP.NET Identity,当我获得声明类型时,我会得到类似

I am using Asp.Net Core and ASP.NET Identity and when I get a Claim type I get something like

"type":"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier",
"value":"123"

如何仅获取简单类型名称,例如:

How to get only the simple type name, e.g.:

"type":"nameidentifier",
"value":"123"

我知道有可能我只是找不到解决方法.

I know this is possible I just can't find the solution.

推荐答案

当我遇到

在关于"页面上检查声明时,您会注意到两件事:有些声明的类型名长于奇数,而声明中的声明超出了您在应用程序中可能需要的数量.

When you inspect the claims on the about page, you will notice two things: some claims have odd long type names and there are more claims than you probably need in your application.

长声明名称来自Microsoft的JWT处理程序,它们试图将某些声明类型映射到.NET的 ClaimTypes 类类型.您可以使用以下代码行(在 Startup 中)关闭此行为.

The long claim names come from Microsoft’s JWT handler trying to map some claim types to .NET’s ClaimTypes class types. You can turn off this behavior with the following line of code (in Startup).

这还意味着您需要将反CSRF保护的配置调整为新的唯一子声明类型:

This also means that you need to adjust the configuration for anti-CSRF protection to the new unique sub claim type:

AntiForgeryConfig.UniqueClaimTypeIdentifier = Constants.ClaimTypes.Subject;
JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>();

我将此代码添加到客户端的 Startup.cs 中,从而缩短了索赔类型.

I added this code to the Startup.cs of my client and it shortened the claimtypes.

更新:

对于 IdentityModel 的较新版本,该属性称为 DefaultInboundClaimTypeMap :

For newer versions of IdentityModel, the property is called DefaultInboundClaimTypeMap:

JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

确保在设置之前先运行此行 .

这篇关于获取简短声明类型名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆