简体   繁体   中英

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 . If you don't want to repeat the whole type, use 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. Then you try to take first element of the empty collection and you get the actual exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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