简体   繁体   English

用户的声明身份

[英]User's Claims Identity

There are currently two kinds of sign on. 目前有两种登录方式。 one is through active directory, and i am able to get the user's claims identity. 一个是通过活动目录,我能够获得用户的声明身份。

the second is through custom registration form, even that i can get user's claims infomration using this code below: 第二种是通过自定义注册表单,即使我可以使用以下代码获取用户的声明信息:

foreach (Microsoft.IdentityModel.Claims.Claim claim in identity.Claims)
{
    Console.Write("Type: " + claim.type);
}

Output: 输出:

claimtype=http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress
value=test@mail.com

The problem I am having is, I am trying to get values of the custom register claims, then save it to user table, using the below code, however no luck 我遇到的问题是,我试图获取自定义寄存器声明的值,然后将其保存到用户表,使用下面的代码,但没有运气

var myemail = identity.Claims.First(c => c.ClaimType == "EmailAddress").Value;

Error says: 错误说:

"Sequence contains no elements" “序列不包含任何元素”

The claim type is 索赔类型是

http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress

not the EmailAddress . 不是EmailAddress If you don't want to repeat the whole type, use ClaimTypes.Email : 如果您不想重复整个类型,请使用ClaimTypes.Email

var myemail = identity.Claims.First(c => c.ClaimType == ClaimTypes.Email).Value

Your error says then that there are no claims of type EmailAddress which is correct. 您的错误表明没有类型EmailAddress是正确的。 Then you try to take first element of the empty collection and you get the actual exception. 然后你尝试获取空集合的第一个元素,你会得到实际的异常。

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

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