Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get email address of Microsoft Live account

Tags:

c#

asp.net-mvc

I'm using mvc5 + c# and i'm gives my user the option to log-in to my website with external login (facebook, google, ...).

I'm trying to add Microsoft Live a as new provider. But, I'm don't see any option to get the email address of the connected user.

I'm Getting those claims when some-microsoft-user is connect ("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 | **************************************************************

There are any option to get the email address of the user, using this information?

like image 863
No1Lives4Ever Avatar asked Nov 23 '13 10:11

No1Lives4Ever


People also ask

How do I find my Microsoft Office email?

Try going to where you first setup your Office at: http://www.office.com/setup It should show you the email address for the Microsoft Account that you used to setup your Office.

How do I find my Microsoft 365 email address?

Apple / Android In the Word, Excel or PowerPoint apps click on the account icon at top right to see the linked accounts. Most likely there's only one, that will be your Office 365/Microsoft linked account. Click on Account to see more details.

What is Windows Live email address?

Windows Live Mail is an email client by Microsoft. It is a part of the Windows Essentials suite and can be downloaded from official website free of cost. It is the version of Windows Mail which succeeded Outlook Express on Windows XP.


1 Answers

Yes, there are. After several hours of trying I managed to get it working like this:

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

Code in AccountController.cs, in function ExternalLoginCallback to retrieve the email address:

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;
like image 161
Tech Avatar answered Oct 27 '22 01:10

Tech