page_type | languages | products | description | urlFragment | ||||
---|---|---|---|---|---|---|---|---|
sample |
|
|
Build a .NET MVC web application that uses WS-Federation to sign-in users from a single Azure Active Directory tenant. |
active-directory-dotnet-webapp-wsfederation |
This sample shows how to build a .Net MVC web application that uses WS-Federation to sign-in users from a single Azure Active Directory tenant, using the ASP.Net WS-Federation OWIN middleware.
The use of WS-Federation is appropriate when you want to maintain a single app codebase that can be deployed either against Azure AD or an on-premises provider such as an Active Directory Federation Services (ADFS) instance. For scenarios in which the app targets exclusively Azure AD (or an OpenID Connect compliant provider) please refer to the WebApp-OpenIdConnect-DotNet sample.
For more information about how the protocols work in this scenario and other scenarios, see Authentication Scenarios for Azure AD.
Getting started is simple! To run this sample you will need:
- Visual Studio 2013
- An Internet connection
- An Azure Active Directory (Azure AD) tenant. For more information on how to get an Azure AD tenant, please see How to get an Azure AD tenant
- A user account in your Azure AD tenant. 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.
From your shell or command line:
git clone https://github.com/Azure-Samples/active-directory-dotnet-webapp-wsfederation.git
- Sign in to the Azure portal.
- On the top bar, click on your account and under the Directory list, choose the Active Directory tenant where you wish to register your application.
- Click on More Services in the left hand nav, and choose Azure Active Directory.
- Click on App registrations and choose Add.
- Enter a friendly name for the application, for example 'WebApp-WSFederation-DotNet' and select 'Web Application and/or Web API' as the Application Type. For the sign-on URL, enter the base URL for the sample, which is by default
https://localhost:44320/
. Click on Create to create the application. - While still in the Azure portal, choose your application, click on Settings and choose Properties.
- Find the Application ID value and copy it to the clipboard.
- For the App ID URI, enter
https://<your_tenant_name>/WebApp-WSFederation-DotNet
, replacing<your_tenant_name>
with the name of your Azure AD tenant. Make sure to remember this value, as you will need it later on when configuring your app 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:Wtrealm
and replace the value with the App ID URI from the Azure portal.
You know what to do!
Click the sign-in link on the homepage of the application to sign-in. On the Azure AD sign-in page, enter the name and password of a user account that is in your Azure AD tenant.
Coming soon.
This sample shows how to use the WS-Federation ASP.Net OWIN middleware to sign-in users from a single Azure AD tenant. The middleware is initialized in the Startup.Auth.cs
file, by passing it the App ID URI of the application and the URL of the Azure AD tenant where the application is registered. The middleware then takes care of:
- Downloading the Azure AD metadata, finding the signing keys, and finding the issuer name for the tenant.
- Processing WS-Federation sign-in responses by validating the signature and issuer in an incoming SAML token, extracting the user's claims, and putting them on ClaimsPrincipal.Current.
- Integrating with the session cookie ASP.Net OWIN middleware to establish a session for the user.
You can trigger the middleware to send a WS-Federation sign-in request by decorating a class or method with the [Authorize]
attribute, or by issuing a challenge,
HttpContext.GetOwinContext().Authentication.Challenge(
new AuthenticationProperties { RedirectUri = "/" },
WSFederationAuthenticationDefaults.AuthenticationType);
Similarly you can send a signout request,
HttpContext.GetOwinContext().Authentication.SignOut(
WSFederationAuthenticationDefaults.AuthenticationType,
CookieAuthenticationDefaults.AuthenticationType);
All of the OWIN middleware in this project is created as a part of the open source Katana project. You can read more about OWIN here.
- In Visual Studio 2013, create a new ASP.Net MVC web application with Authentication set to No Authentication.
- 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 following ASP.Net OWIN middleware NuGets: Microsoft.IdentityModel.Protocol.Extensions, System.IdentityModel.Tokens.Jwt, Microsoft.Owin.Security.WSFederation, Microsoft.Owin.Security.Cookies, Microsoft.Owin.Host.SystemWeb.
- In the
App_Start
folder, create a classStartup.Auth.cs
. You will need to remove.App_Start
from the namespace name. Replace the code for theStartup
class with the code from the same file of the sample app. Be sure to take the whole class definition! The definition changes frompublic class Startup
topublic partial class Startup
. - In
Startup.Auth.cs
resolve missing references by addingusing
statements forOwin
,Microsoft.Owin.Security
,Microsoft.Owin.Security.Cookies
,Microsoft.Owin.Security.WSFederation
,System.Configuration
, andSystem.Globalization
. - Right-click on the project, select Add, select "OWIN Startup class", and name the class "Startup". If "OWIN Startup Class" doesn't appear in the menu, instead select "Class", and in the search box enter "OWIN". "OWIN Startup class" will appear as a selection; select it, and name the class
Startup.cs
. - In
Startup.cs
, replace the code for theStartup
class with the code from the same file of the sample app. Again, note the definition changes frompublic class Startup
topublic partial class Startup
. - In the
Views
-->Shared
folder, create a new partial view_LoginPartial.cshtml
. Replace the contents of the file with the contents of the file of same name from the sample. - In the
Views
-->Shared
folder, replace the contents of_Layout.cshtml
with the contents of the file of same name from the sample. Effectively, all this will do is add a single line,@Html.Partial("_LoginPartial")
, that lights up the previously added_LoginPartial
view. - Create a new empty controller called
AccountController
. Replace the implementation with the contents of the file of same name from the sample. - If you want the user to be required to sign-in before they can see any page of the app, then in the
HomeController
, decorate theHomeController
class with the[Authorize]
attribute. If you leave this out, the user will be able to see the home page of the app without having to sign-in first, and can click the sign-in link on that page to get signed in. - Almost done! Follow the steps in "Running This Sample" to register the application in your AAD tenant.
- In
web.config
, in<appSettings>
, create keys forida:Wtrealm
,ida:AADInstance
, andida:Tenant
and set the values accordingly. For the public Azure AD, the value ofida:AADInstance
ishttps://login.microsoftonline.com
.