获取Microsoft Live帐户的电子邮件地址

No1*_*ver 7 c# asp.net-mvc

我正在使用mvc5 + c#,我通过外部登录(facebook,google,...)为我的用户提供了登录我网站的选项.

我正在尝试将Microsoft Live添加为新的提供商.但是,我没有看到任何选项来获取已连接用户的电子邮件地址.

当某个微软用户连接时,我正在获得这些声明("KEY | VALUE"):

http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier | ***************** 
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name | test 
http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider | ASP.NET Identity 
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier | **************
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name | **************** 
urn:microsoftaccount:id | **************** 
urn:microsoftaccount:name | ****************
urn:microsoftaccount:access_token | **************************************************************
Run Code Online (Sandbox Code Playgroud)

有什么选项可以使用此信息获取用户的电子邮件地址吗?

Tec*_*ech 12

是的,有.经过几个小时的尝试,我设法让它像这样工作:

startup.Auth.cs中的代码

var ms = new Microsoft.Owin.Security.MicrosoftAccount.MicrosoftAccountAuthenticationOptions();
ms.Scope.Add("wl.emails");
ms.Scope.Add("wl.basic");
ms.ClientId = "xxxxxxxxxxxxxxxxxxxxxx";
ms.ClientSecret = "yyyyyyyyyyyyyyyyyyyyy";
ms.Provider = new Microsoft.Owin.Security.MicrosoftAccount.MicrosoftAccountAuthenticationProvider()
{
    OnAuthenticated = async context =>
    {
        context.Identity.AddClaim(new System.Security.Claims.Claim("urn:microsoftaccount:access_token", context.AccessToken));

        foreach (var claim in context.User)
        {
            var claimType = string.Format("urn:microsoftaccount:{0}", claim.Key);
            string claimValue = claim.Value.ToString();
            if (!context.Identity.HasClaim(claimType, claimValue))
                context.Identity.AddClaim(new System.Security.Claims.Claim(claimType, claimValue, "XmlSchemaString", "Microsoft"));
        }
    }
};

app.UseMicrosoftAccountAuthentication(ms);
Run Code Online (Sandbox Code Playgroud)

AccountController.cs中的代码,在函数ExternalLoginCallback中检索电子邮件地址:

string Email = string.Empty;

var externalIdentity = await AuthenticationManager.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);
var emailClaim = externalIdentity.Claims.FirstOrDefault(x => x.Type.Equals(
                                                    "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress",
                                                    StringComparison.OrdinalIgnoreCase));
Email = emailClaim == null ? null : emailClaim.Value;
Run Code Online (Sandbox Code Playgroud)