NULL
NULL

Reputation: 1589

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
[email protected]

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"

Upvotes: 3

Views: 2237

Answers (1)

Wiktor Zychla
Wiktor Zychla

Reputation: 48250

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.

Upvotes: 2

Related Questions