In the sample, an existing web app with its own way of signing in users adds the ability to call an Azure AD protected web API, in this case the Graph API. This sample uses the OAuth 2.0 authorization code grant with confidential client and the Active Directory Authentication Library (ADAL) to obtain access tokens for the web app to call the Graph API with the user's identity.
This sample is useful if you want to add a web API calling ability to an existing application that authenticates Azure AD users using Windows Identity Foundation and WS-Federation. If you want to build a new web application that signs users in using Azure AD as well as calling web APIs protected using Azure AD, check out the WebApp-WebAPI-OpenIDConnect-DotNet sample. OpenIDConnect provides a more efficient way to get access tokens for a user to call a web API, by obtaining an authorization code for the user at the time they sign in. This also means your application can skip having logic for sending OAuth authorization requests and processing OAuth authorization responses.
For more information about how the protocols work in this scenario and other scenarios, see Authentication Scenarios for Azure AD.
To run this sample you will need:
- Visual Studio 2013
- An Internet connection
- An Azure subscription (a free trial is sufficient)
Every Azure subscription has an associated Azure Active Directory tenant. If you don't already have an Azure subscription, you can get a free subscription by signing up at http://wwww.windowsazure.com. All of the Azure AD features used by this sample are available free of charge.
From your shell or command line:
git clone git@github.com:WindowsAzureADSamples/WebApp-WebAPI-OAuth2-UserIdentity-DotNet.git
If you already have a user account in your Azure Active Directory tenant, you can skip to the next step. This sample will not work with a Microsoft account, so if you signed in to the Azure portal with a Microsoft account and have never created a user account in your directory before, you need to do that now. If you create an account and want to use it to sign-in to the Azure portal, don't forget to add the user account as a co-administrator of your Azure subscription.
- Sign in to the Azure management portal.
- Click on Active Directory in the left hand nav.
- Click the directory tenant where you wish to register the sample application.
- Click the Applications tab.
- In the drawer, click Add.
- Click "Add an application my organization is developing".
- Enter a friendly name for the application, for example "WebApp-WebAPI-OAuth2-UserIdentity-DotNet", select "Web Application and/or Web API", and click next.
- For the sign-on URL, enter the base URL for the sample, which is by default
https://localhost:44323/
. - For the App ID URI, enter
https://<your_tenant_name>/WebApp-WebAPI-OAuth2-UserIdentity-DotNet
, replacing<your_tenant_name>
with the name of your Azure AD tenant. Click OK to complete the registration. - While still in the Azure portal, click the Configure tab of your application.
- Find the Client ID value and copy it aside, you will need this later when configuring your application.
- Create a new key for the application. Save the configuration so you can view the key value. Save this aside for when you configure the project in Visual Studio.
- Open the solution in Visual Studio 2013.
- Open the
web.config
file. - Find the app key
ida:Tenant
and replace the value with your AAD tenant name. - Find the app key
ida:ClientId
and replace the value with the Client ID for WebApp-WebAPI-OAuth2-UserIdentity-DotNet from the Azure portal. - Find the app key
ida:AppKey
and replace the value with the key for WebApp-WebAPI-OAuth2-UserIdentity-DotNet from the Azure portal.
Clean the solution, rebuild the solution, and run it.
Explore the sample by creating an account, signing in, clicking the User Profile link, signing out, and starting again.
Coming soon.
Coming soon.
- In Visual Studio 2013, create a new ASP.Net MVC web application called WebApp with Authentication set to Invididual User Accounts.
- Set SSL Enabled to be True. Note the SSL URL.
- In the project properties, Web properties, set the Project Url to be the SSL URL.
- Add the (stable) Active Directory Authentication Library NuGet (
Microsoft.IdentityModel.Clients.ActiveDirectory
), version 1.0.3 (or higher). - In the
Models
folder add a new class calledUserProfile.cs
. Copy the implementation of UserProfile from this sample into the class. - Add a new empty MVC5 controller UserProfileController to the project. Copy the implementation of the controller from the sample. Remember to include the [Authorize] attribute on the class definition.
- In
Views
-->UserProfile
create a new view,Index.cshtml
, and copy the implementation from this sample. - In the shared
_Layout
view, add the Action Link for Profile that is in the sample. - Add a new empty MVC5 controller OAuthController to the project. Copy the implemementation of the controller from the sample.
- Open the AccountController controller, find the
LogOff()
method, and add this line at the beginning of the method:OAuthController.RemoveAllFromCache();
. Also note the comments that are included in the sample file in theLogOff()
method. - In
web.config
, in<appSettings>
, create keys forida:ClientId
,ida:AppKey
,ida:AADInstance
,ida:Tenant
,ida:GraphResourceId
, andida:GraphUserUrl
and set the values accordingly. For the public Azure AD, the value ofida:AADInstance
ishttps://login.windows.net/{0}
the value ofida:GraphResourceId
ishttps://graph.windows.net
, and the value ofida:GraphUserUrl
ishttps://graph.windows.net/{0}/me?api-version=2013-11-08
.