Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get E-mail of User Authenticated with Microsoft Account in ASP.NET Identity

I'm using the ASP.NET Identity stuff that came with the new MVC 5 templates in VS2013. I've configured external login providers so people can sign up using Google, Facebook, or Microsoft. However, I would still like to get peoples' e-mail address (e.g. for notifications, updates, etc.).

By default the app.UseGoogleAuthentication() will also request the user's e-mail address. For Facebook, I've created a new instance of the FacebookAuthenticationOptions class, and added the following scope: facebook.Scope.Add("email"). This also works.

I'm having problems getting the e-mail for people using a Microsoft Account. The MicrosoftAccountAuthenticationOptions also has a Scope property, but adding email doesn't seem to work. In the documentation I see there is a scope wl.emails but it returns an array of e-mail addresses and I'm not sure if this is the equivalent for email with Facebook.

Does anyone have a suggestion how to get the e-mail address as a claim when authenticating?

like image 409
Knelis Avatar asked Mar 06 '14 15:03

Knelis


1 Answers

  1. Configure the scopes for Microsoft.

    var mo = new MicrosoftAccountAuthenticationOptions
    {
        Caption = "Live",
        ClientId = clientId,
        ClientSecret = clientSecret,
    };
    
    mo.Scope.Add("wl.basic");
    mo.Scope.Add("wl.emails");
    
    app.UseMicrosoftAccountAuthentication(mo);
    
  2. Grab the email claim

    var identity = await AuthenticationManager.AuthenticateAsync(DefaultAuthenticationTypes.ExternalCookie);
    var emailClaim = identity.Identity.FindFirst(ClaimTypes.Email);
    

Hope this helps you.

like image 64
Chris Martin Avatar answered Oct 20 '22 20:10

Chris Martin