AspNetCore.Docs/aspnetcore/security/authentication/azure-ad-b2c.md

134 lines
9.9 KiB
Markdown
Raw Normal View History

2018-01-18 09:32:53 +08:00
---
title: Cloud authentication with Azure Active Directory B2C
author: camsoper
description: Shows how to set up Azure Active Directory B2C with ASP.NET Core.
2018-01-18 09:32:53 +08:00
ms.author: casoper
manager: wpickett
ms.date: 01/12/2018
ms.topic: tutorial
2018-01-18 09:32:53 +08:00
ms.technology: aspnet
ms.prod: asp.net-core
uid: security/authentication/azure-ad-b2c
custom: mvc
2018-01-18 09:32:53 +08:00
---
# Cloud authentication with Azure Active Directory B2C
By [Cam Soper](https://twitter.com/camsoper)
[Azure Active Directory B2C](/azure/active-directory-b2c/active-directory-b2c-overview) (Azure AD B2C) is a cloud identity management solution for your web and mobile apps. The service provides global, scalable, cloud-based authentication to web apps and APIs using individual accounts, social network accounts, and federated enterprise accounts for apps hosted in the cloud and on-premises. Additionally, Azure AD B2C can provide multifactor authentication with very little configuration.
2018-01-18 09:32:53 +08:00
2018-01-19 07:20:49 +08:00
In this tutorial, you will learn how to:
> [!div class="checklist"]
> * Create an Azure Active Directory B2C tenant
> * Register an app in Azure AD B2C
2018-01-19 07:20:49 +08:00
> * Use Visual Studio to create an ASP.NET Core Web Application configured to use the Azure AD B2C tenant for authentication
> * Configure policies controlling the behavior of the Azure AD B2C tenant
2018-01-18 09:32:53 +08:00
## Prerequisites
The following are required for this walkthrough:
* A [Microsoft Azure subscription](https://azure.microsoft.com/free/?ref=microsoft.com&utm_source=microsoft.com&utm_medium=docs&utm_campaign=visualstudio).
2018-01-19 07:17:48 +08:00
* [Visual Studio 2017](https://aka.ms/vsdownload?utm_source=mscom&utm_campaign=msdocs) (any edition).
2018-01-18 09:32:53 +08:00
## Create the Azure Active Directory B2C tenant
Create an Azure Active Directory B2C tenant [as described in the documentation](/azure/active-directory-b2c/active-directory-b2c-get-started). When prompted, associating the tenant with an Azure subscription is optional for this tutorial.
2018-01-18 09:32:53 +08:00
## Register the app in Azure AD B2C
2018-01-18 09:32:53 +08:00
In the newly created Azure AD B2C tenant, register your app using [the steps in the documentation](/azure/active-directory-b2c/active-directory-b2c-app-registration#register-a-web-app) under the **Register a web app** section. Stop at the **Create a web app client secret** section. A client secret isn't required for this tutorial.
2018-01-18 09:32:53 +08:00
Use the following values:
| Setting | Value | Notes |
|-------------------------------|---------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| **Name** | *\<app name\>* | Enter a **Name** for the app that describes your app to consumers. |
| **Include web app / web API** | Yes | |
| **Allow implicit flow** | Yes | |
| **Reply URL** | `https://localhost:44300` | Reply URLs are endpoints where Azure AD B2C returns any tokens that your app requests. Visual Studio provides the Reply URL to use. For now, enter `https://localhost:44300` to complete the form. |
| **App ID URI** | Leave blank | Not required for this tutorial. |
| **Include native client** | No | |
2018-01-18 09:32:53 +08:00
> [!WARNING]
> If setting up a non-localhost Reply URL, be aware of the [constraints on what is allowed in the Reply URL list](/azure/active-directory-b2c/active-directory-b2c-app-registration#choosing-a-web-app-or-api-reply-url).
2018-01-18 09:32:53 +08:00
After the app is registered, the list of apps in the tenant is displayed. Select the app that was just registered. Select the **Copy** icon to the right of the **Application ID** field to copy the Application ID to the clipboard.
2018-01-18 09:32:53 +08:00
Nothing more can be configured in the Azure AD B2C tenant at this time, but leave the browser window open. There will be more configuration after the ASP.NET Core app is created.
2018-01-18 09:32:53 +08:00
## Create an ASP.NET Core app in Visual Studio 2017
2018-01-18 09:32:53 +08:00
The Visual Studio Web Application template can be configured to use the Azure AD B2C tenant for authentication.
2018-01-18 09:32:53 +08:00
In Visual Studio:
1. Create a new ASP.NET Core Web Application.
2. Select **Web Application** from the list of templates.
2018-01-18 09:32:53 +08:00
3. Select the **Change Authentication** button.
![Change Authentication Button](./azure-ad-b2c/_static/changeauth.png)
4. In the **Change Authentication** dialog, select **Individual User Accounts**, and then select **Connect to an existing user store in the cloud** in the dropdown.
![Change Authentication Dialog](./azure-ad-b2c/_static/changeauthdialog.png)
5. Complete the form with the following values:
| Setting | Value |
|-------------------------------|---------------------------------------------------|
| **Domain Name** | *\<the domain name of your B2C tenant\>* |
| **Application ID** | *\<paste the Application ID from the clipboard\>* |
| **Callback Path** | *\<use the default value\>* |
| **Sign-up or sign-in policy** | `B2C_1_SiUpIn` |
| **Reset password policy** | `B2C_1_SSPR` |
| **Edit profile policy** | *\<leave blank\>* |
2018-01-18 09:32:53 +08:00
Select the **Copy** link next to **Reply URI** to copy the Reply URI to the clipboard. Select **OK** to close the **Change Authentication** dialog, and then select **OK** to create the web app.
2018-01-18 09:32:53 +08:00
## Finish the B2C app registration
2018-01-18 09:32:53 +08:00
Return to the browser window with the B2C app properties still open. Change the temporary **Reply URL** specified earlier to the value copied from Visual Studio, and then select **Save** at the top of the window.
2018-01-18 09:32:53 +08:00
> [!TIP]
> If you didn't copy the Reply URL, use the SSL address from the Debug tab in the web project properties, and append the **CallbackPath** value from *appsettings.json*.
2018-01-18 09:32:53 +08:00
## Configure policies
Use the steps in the Azure AD B2C documentation to [create a sign-up or sign-in policy](/azure/active-directory-b2c/active-directory-b2c-reference-policies#create-a-sign-up-or-sign-in-policy), and then [create a password reset policy](/azure/active-directory-b2c/active-directory-b2c-reference-policies#create-a-password-reset-policy). Use the example values provided in the documentation for **Identity providers**, **Sign-up attributes**, and **Application claims**. Using the **Run now** button to test the policies as described in the documentation is optional.
2018-01-18 09:32:53 +08:00
> [!WARNING]
> Ensure the policy names are exactly as described in the documentation, as those policies were used in the **Change Authentication** dialog in Visual Studio. The policy names can be verified in *appsettings.json*.
2018-01-18 09:32:53 +08:00
## Run the app
2018-01-18 09:32:53 +08:00
In Visual Studio, press **F5** to build and run the app. After the web app launches, select **Sign in**.
2018-01-18 09:32:53 +08:00
![Sign into the app](./azure-ad-b2c/_static/signin.png)
2018-01-18 09:32:53 +08:00
The browser redirects to the Azure AD B2C tenant. Sign in with an existing account (if one was created testing the policies) or select **Sign up now** to create a new account. The **Forgot your password?** link is used to reset a forgotten password.
2018-01-18 09:32:53 +08:00
![Azure AD B2C login](./azure-ad-b2c/_static/b2csts.png)
2018-01-18 09:32:53 +08:00
After successfully signing in, the browser redirects to the web app.
![Success](./azure-ad-b2c/_static/success.png)
## Next steps
In this tutorial, you will learned how to:
> [!div class="checklist"]
> * Create an Azure Active Directory B2C tenant
> * Register an app in Azure AD B2C
2018-01-19 07:20:49 +08:00
> * Use Visual Studio to create an ASP.NET Core Web Application configured to use the Azure AD B2C tenant for authentication
> * Configure policies controlling the behavior of the Azure AD B2C tenant
Now that the ASP.NET Core app is configured to use Azure Active Directory B2C for authentication, the [Authorize attribute](xref:security/authorization/simple) can be used to secure your app. Continue developing your app by learning to:
2018-01-18 09:32:53 +08:00
* [Customize the Azure AD B2C user interface](/azure/active-directory-b2c/active-directory-b2c-reference-ui-customization)
* [Configure password complexity requirements](/azure/active-directory-b2c/active-directory-b2c-reference-password-complexity)
* [Enable multi-factor authentication](/azure/active-directory-b2c/active-directory-b2c-reference-mfa)
* Configure additional identity providers, such as [Microsoft](/azure/active-directory-b2c/active-directory-b2c-setup-msa-app), [Facebook](/azure/active-directory-b2c/active-directory-b2c-setup-fb-app), [Google](/azure/active-directory-b2c/active-directory-b2c-setup-goog-app), [Amazon](/azure/active-directory-b2c/active-directory-b2c-setup-amzn-app), [Twitter](/azure/active-directory-b2c/active-directory-b2c-setup-twitter-app), and others.
* [Use the Azure AD Graph API](/azure/active-directory-b2c/active-directory-b2c-devquickstarts-graph-dotnet) to retrieve additional user information, such as group membership, from the Azure AD B2C tenant.