Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm using the new WIF 4.5 SDK, but i have the same annoying exception that with LINQ is resolved with .FirstorDefault()

 var claimEmail = ClaimsPrincipal.Current.FindFirst(ClaimTypes.Email).Value;

The problem here is that Windows Live ID have no email value in the claim, so if an user log in with a live ID i have a NullReferenceException - Object reference not set to an instance of an object. I also tried;

var claimEmail = ClaimsPrincipal.Current.FindFirst(ClaimTypes.Email).Value.FirstorDefault();

without success

How can i return NULL or "" if ther's no email in the claim?

Thanks

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

There isn't a FirstOrDefault shortcut in the ClaimsPrincipal class, but you can always just use LINQ to iterate the list of claims to do the same thing:

var claimEmail = ClaimsPrincipal.Current.Claims.Where(c => c.Type == ClaimTypes.Email).FirstOrDefault();

Or you can just put in a check to make sure the Claim isn't NULL:

var claimEmail = ClaimsPrincipal.Current.FindFirst(ClaimTypes.Email);
var email = (claimEmail == null ? string.Empty : claimEmail.Value);

Hopefully this helps.

share|improve this answer
 
The first statement is valid, even if do not take the value, but i can parse the string to take it. Thank you Sir! I also resolved inserting the code in a try/catch block but was not an elegant way –  Light Jul 26 '12 at 0:58
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.