简体   繁体   中英

how can I use a Microsoft Account to authenticate to my website

I have a website where a users identity is needed, I'd really prefer not to make them create yet another username/password combo that they have to remember

are there SDK's for allowing authentication from an Microsoft account?

That's rather easy as a default empty template of an ASP.NET 4.5 website shows how to have OAuth2 authentication with google/facebook/liveid/twitter.

http://www.asp.net/aspnet/overview/aspnet-45/oauth-in-the-default-aspnet-45-templates

Check out the Principal Context class. You can create it using a localhost (Machine) or domain context and use the ValidateCrentials(string username, string password) method to authenticate using Windows credentials.

http://msdn.microsoft.com/en-us/library/bb154889.aspx

Here's how I've used it in my website. (Put this in a POST method of your authentication controller or something)

The code below will take a username say "bob" or "localhost\\bob" or "DOMAIN\\bob" etc., and get the right PrincipalContext for authenticating the user. NOTE: it's case insensitive here.

        public bool ValidateCredentials(string username, System.Security.SecureString password)
    {
        string domain = Environment.MachineName;
        if (username.Contains("\\"))
        {
            domain = username.Split('\\')[0];
            username = username.Split('\\')[1];
        }

        if (domain.Equals("localhost", StringComparison.CurrentCultureIgnoreCase))
            domain = Environment.MachineName;

        if (domain.Equals(Environment.MachineName, StringComparison.CurrentCultureIgnoreCase))
            using (PrincipalContext context = new PrincipalContext(ContextType.Machine))
            {
                return context.ValidateCredentials(username, password.ToUnsecureString());
            }
        else
            using(PrincipalContext context = new PrincipalContext(ContextType.Domain))
            {
                //return context.ValidateCredentials(domain + "\\" + username, password.ToUnsecureString());
                return context.ValidateCredentials(username, password.ToUnsecureString());
            }


    }

Microsoft provides the Live Connect SDK for integration Microsoft services into your applications, including the Microsoft Accounts identity provider.

There is a specific example on Server-Side Scenarios which should cover all you need to get integrated.

Do you mean from an active directory windows account? If so you could use windows authentication and just have the index page sign them in automatically.

http://msdn.microsoft.com/en-us/library/ff647405.aspx

Use the following commands in your code behind file to get the relevant information for signing in:

System.Security.Principal.WindowsIdentity.GetCurrent().Name
User.Identity.IsAuthenticated
User.Identity.AuthenticationType
User.Identity.Name

The amount of changes / rebranding / deprecation / dead links from Microsoft drives me crazy. In any case, the latest version of this from what I've found is "Microsoft Account external login", which can be first set up on the Microsoft Developer Portal .

I found a guide that explains how to do this for .Net Core at https://docs.microsoft.com/en-us/aspnet/core/security/authentication/social/microsoft-logins , though the first half (eg setting the Redirect URI) isn't framework-specific.

I also found some relevant source code for .Net Core at https://github.com/aspnet/Security/blob/master/src/Microsoft.AspNetCore.Authentication.MicrosoftAccount/MicrosoftAccountOptions.cs , which shows some of the Claims (user details) that are retrieved:

ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "id");
ClaimActions.MapJsonKey(ClaimTypes.Name, "displayName");
ClaimActions.MapJsonKey(ClaimTypes.GivenName, "givenName");
ClaimActions.MapJsonKey(ClaimTypes.Surname, "surname");
ClaimActions.MapCustomJson(ClaimTypes.Email,
    user => user.Value<string>("mail") ?? user.Value<string>("userPrincipalName"));

The support from the latest version of .Net Core suggests to me that this external login API still works. I haven't tested them out yet, I will update if I get to do this login integration.

Answer Expired - Microsoft had changed there links


Finally found the Javascript library to do it similar to Facebook/Google Authentication

https://msdn.microsoft.com/en-au/library/ff748792.aspx

And

http://isdk.dev.live.com/dev/isdk/ISDK.aspx?category=scenarioGroup_core_concepts&index=0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM