Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using OAuth to connect to a Windows Azure Active Directory

In the scaffolding for an ASP.NET MVC project, the StartUp.Auth.cs file currently contains this code:

public partial class Startup
{
    // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
    public void ConfigureAuth(IAppBuilder app)
    {
        // Enable the application to use a cookie to store information for the signed in user
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login")
        });
        // Use a cookie to temporarily store information about a user logging in with a third party login provider
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

        // Uncomment the following lines to enable logging in with third party login providers
        app.UseMicrosoftAccountAuthentication(
            clientId: "0000000000000000",
            clientSecret: "xxxx-xxxxxxxxxxxxxxxxxxx-xxxxxxx");

        //app.UseTwitterAuthentication(
        //   consumerKey: "",
        //   consumerSecret: "");

        //app.UseFacebookAuthentication(
        //   appId: "",
        //   appSecret: "");

        //app.UseGoogleAuthentication();
    }
}

Uncommenting the app.UseXxxAuthentication() lines and adding in your provider's key and secret gives you the ability to use the respective providers to perform OAuth logins. Under the covers, these methods use classes derived from the Owin class AuthenticationMiddleware.

I have looked on the web, but I cannot find a custom implementation of AuthenticationMiddleware that links directly to a Windows Azure Active Directory instance. Are there any such implementations?

Is this the right way to use OAuth to connect to my Windows Azure Active Directory instance?

like image 261
Rob Lyndon Avatar asked Dec 18 '13 14:12

Rob Lyndon


People also ask

Does OAuth support Active Directory?

Azure Active Directory (Azure AD) supports all OAuth 2.0 flows.

Does Azure AD support OAuth?

Azure AD supports two different OAuth flows in which an OAuth Client can get an access token. The authorization server can grant the OAuth client an access token on behalf of the user. The authorization server can grant the OAuth client an access token for the OAuth client itself.

How does OAuth2 work Azure?

The OAuth 2.0 authorization code grant type, or auth code flow, enables a client application to obtain authorized access to protected resources like web APIs. The auth code flow requires a user-agent that supports redirection from the authorization server (the Microsoft identity platform) back to your application.

Does Azure AD use SAML or OAuth?

SAML authentication is commonly used with identity providers such as Active Directory Federation Services (AD FS) federated to Azure AD, so it's often used in enterprise applications.


Video Answer


3 Answers

You should be able to go to your Package Manager, and NuGet import the Katana Owin implementations for Windows Azure AD, which will be listed as Microsoft.Owin.Security.ActiveDirectory This is the middleware that enables an application to use Microsoft's technology for authentication. The current version as of this post is 2.0.2

Once you have that, you should be able to leverage the middleware for AD and ADFS 2.1 oAuth tokens like so:

WindowsAzureActiveDirectoryBearerAuthenticationOptions myoptions = new WindowsAzureActiveDirectoryBearerAuthenticationOptions();
        myoptions.Audience = "https://login.windows.net/myendpoint";
        myoptions.Tenant = "mydirectory.onmicrosoft.com";
        myoptions.AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Passive;
        app.UseWindowsAzureActiveDirectoryBearerAuthentication(myoptions);

That should give you the ability to have the Owin middleware use Windows Azure AD Bearer Authentication in this scenario.

Happy coding!

like image 159
user3188957 Avatar answered Sep 27 '22 22:09

user3188957


I don't believe you can use WAAD in this way. Microsoft Account is for what used to be Windows Live ID (More information here), and this is different from WAAD. And the OAuth implementation in WAAD is not complete yet and in preview (more details here). The best way to use WAAD today is via WS-Federation / WIF.

The pain point in VS 2013 is that you can't do it easily manually, nor you can change the selected authentication once you created the project.

The easiest way to get the required configuration is to go and create new web app, and change the authentication. Chose Change Authentication at the very first step of the wizard (where you select the type of App - MVC, WebAPI, etc.). Then choose Organizational Account. It has only one option - Cloud single organization - enter your tenant domain name (may be the xxxx.onmicrosoft.com). And chose access level (Single Sign On, SSO + read directory data, SSO + read + write directory data). Next you will be asked to sign in with account which is Global Administrator in this Active Directory. The wizard will create necessary web.confg changes and Identity configuration. There still no support in OWIN for WAAD, and it will create a new IdentityConfig.cs instead Startup.Auth.cs file. You can then copy generated files and web.config changes into your project. You can still combine WAAD with other providers and OWIN, but this still requires more advanced skills.

It is a little more complicated that it should be. But things may change for good in the future.

like image 34
astaykov Avatar answered Sep 27 '22 22:09

astaykov


There is a new Owin middleware that adds Ws Federation authentication to your site with a few simple lines of code much like the individual account examples in the new MVC project template. It's currently in alpha but here is a link to an article explaining how to create your app in Windows Azure Active Directory and configure the OWIN middleware.

However this uses cookie authentication rather than OAuth tokens but this should be sufficient for a pure ASP MVC site.

like image 36
Jason Dorell Avatar answered Sep 27 '22 23:09

Jason Dorell