【问题标题】:Get email address of Microsoft Live account获取 Microsoft Live 帐户的电子邮件地址
【发布时间】:2013-11-23 10:01:42
【问题描述】:

我正在使用 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 | **************************************************************

是否有任何选项可以使用此信息获取用户的电子邮件地址?

【问题讨论】:

标签: c# asp.net-mvc


【解决方案1】:

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

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);

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;

【讨论】:

  • 很好的答案,不幸的是我的项目在 MVC4 中,我们没有使用 OWin,而是使用 Oauth,所以你有类似的 Oauth 解决方案吗?
【解决方案2】:

对于在向您的站点添加 Microsoft 登录时遇到此问题的任何人:

我发现我只能获取我的 Microsoft Work/School 帐户的电子邮件地址。我用我的@gmail.com 地址和一个@outlook.com 创建了一个Microsoft 帐户。当我查询他们的信息时,两者都没有关联的任何电子邮件信息。但是,对于符合条件的地址,您可以通过 GET 请求获取电子邮件信息:

https://apis.live.net/v5.0/me?access_token=ACCESS_TOKEN

其中 ACCESS_TOKEN 是经过身份验证的用户将提供给您的内容*。有关此 API 的更多信息,您可以查看此链接:https://msdn.microsoft.com/en-us/library/office/dn659736.aspx

基本上,调用 Microsoft Live API (apis.live.net) 将为您提供用户已授予您访问权限的信息(因此,如果您在应用中启用了 wl.emails 范围,则应该查看他们的电子邮件地址)。

*或者,当用户使用 Microsoft 登录名登录您的站点时,他们对您站点的请求将有一个标题,其中包含访问令牌和可能的电子邮件地址(电子邮件地址位于标题“X-MS- CLIENT-PRINCIPAL-NAME”,访问令牌也在“X-MS-TOKEN-MICROSOFT-ACCESS-TOKEN”下的 HTTP 标头中)。

【讨论】:

    猜你喜欢
    • 2018-03-06
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-01
    • 2014-10-28
    • 1970-01-01
    相关资源
    最近更新 更多