Yesterday I talked about a bug which prevented me to complete the authorization grant flow with Azure AD. It turn out the bug is only exposed when using Azure Management Portal for Relying party registration. In this post, I’ll use Graph Explorer to do the registration which works fine.
My scenario is to create a simple MVC application which would do the user authentication against the Azure AD.
Once the user is signed in, the web app then acquires an “access” & “refresh token” for the Graph API (I’ll work with other resources in future) using the 3-leg authorization grant flow.
I started by creating an empty MVC 4.0 application and added a home controller with a simple view displaying the identity & claims of the authenticated user.
Running the app gave me the url which I would use to register my app with Azure AD using Graph Explorer. Registration instruction are available in this blog post under the ‘Setting up permissions’ section. My registration settings looks like this
Now back to VS and using the “Identity & Access”, I have externalized the authentication of my app to windows azure AD.
The tooling does all the magic and generates required WIF configuration.
Now when I run my application, I’m redirected to azure AD for authentication and after signing in, I’m back in my app with following claims.
The Authorize action simply redirects to the authorization endpoint requesting an ‘authorization code’.
I’m passing Authorized action as the redirect_uri to Azure AD. That’s where AD would send me the ‘authorization code'.
Clicking the ‘Authorize’ link takes me to Azure Ad & after authentication AD redirects my browser back to Authorized action with an authorization code.
From the Authorized controller action I fire a post request which gives me access and refresh tokens back. Please note, the redirect_uri MUST be same for both authorization code and token request.
The access & refresh tokens are scoped to Graph API in this case. I can now attach this “access token” to my requests to graph API to read/write the directory data. There are few samples on this topic already so I’m not going to cover that in this post.