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

ε祈祈猫儿з 提交于 2019-12-18 21:13:12

问题


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:


  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.




回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!