问题
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?
回答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);Grab the email claim
var identity = await AuthenticationManager.AuthenticateAsync(DefaultAuthenticationTypes.ExternalCookie); var emailClaim = identity.Identity.FindFirst(ClaimTypes.Email);
Hope this helps you.
回答2:
app.UseMicrosoftAccountAuthentication(new MicrosoftAccountAuthenticationOptions()
{
ClientId = "Your_client_id",
ClientSecret = "your_client_secret_key",
Scope = { "wl.basic", "wl.emails" }
});
and to get email
var externalIdentity = HttpContext.GetOwinContext().Authentication.GetExternalLoginInfoAsync();
string email=externalIdentity.Result.Email;
来源:https://stackoverflow.com/questions/22229593/get-e-mail-of-user-authenticated-with-microsoft-account-in-asp-net-identity