简体   繁体   English

如何在cSharp中检索特定格式的声明?

[英]How to retrieve claims in specific format in c sharp?

I have the below code that retrieves claims for my user after authentication when doing unit test:在进行单元测试时,我有以下代码在身份验证后为我的用户检索声明:

        var claims = new List<Claim>()
        {
            new Claim(ClaimTypes.GivenName, "John"),
            new Claim(ClaimTypes.Surname, "Doe"),
            new Claim(ClaimTypes.Email, "john.doe@gmail.com"),
        };

The above code returns the claims in the following format -eg for GivenName :上面的代码以以下格式返回声明 -eg for GivenName

{http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname: John}

But my controller expects it in this format:但是我的控制器期望它采用这种格式:

{firstname: John}

Is there a way to adjust my code to return it as per the controller requirement?有没有办法调整我的代码以根据控制器要求返回它?

Also the claim in my controller has the firstname tag but there is no such tag when creating dummy claim in the unit test, instead of firstname, it only allows me to use givenname tag, is there a way to change this?我的控制器中的声明也有名字标签,但是在单元测试中创建虚拟声明时没有这样的标签,而不是名字,它只允许我使用 givenname 标签,有没有办法改变这个?

The claim is basically just a key/value pair and allows you to construct it like this:声明基本上只是一个键/值对,并允许您像这样构造它:

new Claim("firstname", "John")

See MSDN .请参阅MSDN


With respect to this:关于这一点:

Also the claim in my controller has the firstname tag but there is no such tag when creating dummy claim in the unit test, instead of firstname, it only allows me to use givenname tag, is there a way to change this?我的控制器中的声明也有名字标签,但是在单元测试中创建虚拟声明时没有这样的标签,而不是名字,它只允许我使用 givenname 标签,有没有办法改变这个?

Depending on what the unit test is actually testing, this might be an indication that something is wrong.根据单元测试实际测试的内容,这可能表明出现了问题。 If in doubt, it's best to consult with the author of see the documentation.如果有疑问,最好咨询查看文档的作者。

Do note: the fact it doesn't exit in the unit-tests does not necessarily means its wrong - you should have a closer look what the test is actually about.请注意:它没有在单元测试中退出的事实并不一定意味着它是错误的 - 您应该仔细查看测试的实际内容。

please try this请试试这个

var claims = new List<Claim>()
            {
                new Claim("firstname", "John"),
                new Claim("surname", "Doe"),
                new Claim("email", "john.doe@gmail.com"),
            };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM