prep identity for V6 monikers (#23268)
* prep identity for V6 monikers * prep identity for V6 monikerspull/23271/head
parent
612315e292
commit
b4050da8ef
|
@ -9,7 +9,8 @@ uid: security/authentication/identity
|
|||
---
|
||||
# Introduction to Identity on ASP.NET Core
|
||||
|
||||
::: moniker range=">= aspnetcore-3.0"
|
||||
<!-- DO NOT PR this until RC1 branch merges -->
|
||||
::: moniker range=">= aspnetcore-6.0"
|
||||
|
||||
By [Rick Anderson](https://twitter.com/RickAndMSFT)
|
||||
|
||||
|
@ -106,22 +107,6 @@ Run the app and register a user. Depending on your screen size, you might need t
|
|||
|
||||
Services are added in `ConfigureServices`. The typical pattern is to call all the `Add{Service}` methods, and then call all the `services.Configure{Service}` methods.
|
||||
|
||||
::: moniker-end
|
||||
|
||||
::: moniker range=">= aspnetcore-3.0 < aspnetcore-5.0"
|
||||
|
||||
[!code-csharp[](identity/sample/WebApp3/Startup.cs?name=snippet_configureservices&highlight=11-99)]
|
||||
|
||||
The preceding highlighted code configures Identity with default option values. Services are made available to the app through [dependency injection](xref:fundamentals/dependency-injection).
|
||||
|
||||
Identity is enabled by calling <xref:Microsoft.AspNetCore.Builder.AuthAppBuilderExtensions.UseAuthentication*>. `UseAuthentication` adds authentication [middleware](xref:fundamentals/middleware/index) to the request pipeline.
|
||||
|
||||
[!code-csharp[](identity/sample/WebApp3/Startup.cs?name=snippet_configure&highlight=19)]
|
||||
|
||||
::: moniker-end
|
||||
|
||||
::: moniker range=">= aspnetcore-5.0"
|
||||
|
||||
[!code-csharp[](identity/sample/WebApp5x/Startup.cs?name=snippet_configureservices&highlight=12-99)]
|
||||
|
||||
The preceding code configures Identity with default option values. Services are made available to the app through [dependency injection](xref:fundamentals/dependency-injection).
|
||||
|
@ -130,10 +115,6 @@ Identity is enabled by calling [UseAuthentication](/dotnet/api/microsoft.aspnetc
|
|||
|
||||
[!code-csharp[](identity/sample/WebApp5x/Startup.cs?name=snippet_configure&highlight=19)]
|
||||
|
||||
::: moniker-end
|
||||
|
||||
::: moniker range=">= aspnetcore-3.0"
|
||||
|
||||
The template-generated app doesn't use [authorization](xref:security/authorization/secure-data). `app.UseAuthorization` is included to ensure it's added in the correct order should the app add authorization. `UseRouting`, `UseAuthentication`, `UseAuthorization`, and `UseEndpoints` must be called in the order shown in the preceding code.
|
||||
|
||||
For more information on `IdentityOptions` and `Startup`, see <xref:Microsoft.AspNetCore.Identity.IdentityOptions> and [Application Startup](xref:fundamentals/startup).
|
||||
|
@ -275,30 +256,36 @@ To prevent publishing static Identity assets (stylesheets and JavaScript files f
|
|||
|
||||
::: moniker-end
|
||||
|
||||
::: moniker range="< aspnetcore-3.0"
|
||||
::: moniker range=">= aspnetcore-3.0 < aspnetcore-6.0"
|
||||
|
||||
By [Rick Anderson](https://twitter.com/RickAndMSFT)
|
||||
|
||||
ASP.NET Core Identity is a membership system that adds login functionality to ASP.NET Core apps. Users can create an account with the login information stored in Identity or they can use an external login provider. Supported external login providers include [Facebook, Google, Microsoft Account, and Twitter](xref:security/authentication/social/index).
|
||||
ASP.NET Core Identity:
|
||||
|
||||
Identity can be configured using a SQL Server database to store user names, passwords, and profile data. Alternatively, another persistent store can be used, for example, Azure Table Storage.
|
||||
* Is an API that supports user interface (UI) login functionality.
|
||||
* Manages users, passwords, profile data, roles, claims, tokens, email confirmation, and more.
|
||||
|
||||
[View or download the sample code](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/security/authentication/identity/sample/src/ASPNETCore-IdentityDemoComplete/) ([how to download](xref:index#how-to-download-a-sample)).
|
||||
Users can create an account with the login information stored in Identity or they can use an external login provider. Supported external login providers include [Facebook, Google, Microsoft Account, and Twitter](xref:security/authentication/social/index).
|
||||
|
||||
In this topic, you learn how to use Identity to register, log in, and log out a user. For more detailed instructions about creating apps that use Identity, see the Next Steps section at the end of this article.
|
||||
[!INCLUDE[](~/includes/requireAuth.md)]
|
||||
|
||||
The [Identity source code](https://github.com/dotnet/AspNetCore/tree/main/src/Identity) is available on GitHub. [Scaffold Identity](xref:security/authentication/scaffold-identity) and view the generated files to review the template interaction with Identity.
|
||||
|
||||
Identity is typically configured using a SQL Server database to store user names, passwords, and profile data. Alternatively, another persistent store can be used, for example, Azure Table Storage.
|
||||
|
||||
In this topic, you learn how to use Identity to register, log in, and log out a user. Note: the templates treat username and email as the same for users. For more detailed instructions about creating apps that use Identity, see [Next Steps](#next).
|
||||
|
||||
[Microsoft identity platform](/azure/active-directory/develop/) is:
|
||||
|
||||
* An evolution of the Azure Active Directory (Azure AD) developer platform.
|
||||
* Unrelated to ASP.NET Core Identity.
|
||||
|
||||
[!INCLUDE[](~/includes/IdentityServer4.md)]
|
||||
|
||||
[View or download the sample code](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/security/authentication/identity/sample) ([how to download](xref:index#how-to-download-a-sample)).
|
||||
|
||||
<a name="adi"></a>
|
||||
|
||||
## AddDefaultIdentity and AddIdentity
|
||||
|
||||
<xref:Microsoft.Extensions.DependencyInjection.IdentityServiceCollectionUIExtensions.AddDefaultIdentity*> was introduced in ASP.NET Core 2.1. Calling `AddDefaultIdentity` is similar to calling the following:
|
||||
|
||||
* <xref:Microsoft.Extensions.DependencyInjection.IdentityServiceCollectionExtensions.AddIdentity*>
|
||||
* <xref:Microsoft.AspNetCore.Identity.IdentityBuilderUIExtensions.AddDefaultUI*>
|
||||
* <xref:Microsoft.AspNetCore.Identity.IdentityBuilderExtensions.AddDefaultTokenProviders*>
|
||||
|
||||
See [AddDefaultIdentity source](https://github.com/dotnet/AspNetCore/blob/release/2.1/src/Identity/UI/src/IdentityServiceCollectionUIExtensions.cs#L47-L63) for more information.
|
||||
|
||||
## Create a Web app with authentication
|
||||
|
||||
Create an ASP.NET Core Web Application project with Individual User Accounts.
|
||||
|
@ -316,6 +303,12 @@ Create an ASP.NET Core Web Application project with Individual User Accounts.
|
|||
dotnet new webapp --auth Individual -o WebApp1
|
||||
```
|
||||
|
||||
The preceding command creates a Razor web app using SQLite. To create the web app with LocalDB, run the following command:
|
||||
|
||||
```dotnetcli
|
||||
dotnet new webapp --auth Individual -uld -o WebApp1
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
The generated project provides [ASP.NET Core Identity](xref:security/authentication/identity) as a [Razor Class Library](xref:razor-pages/ui-class). The Identity Razor Class Library exposes endpoints with the `Identity` area. For example:
|
||||
|
@ -332,12 +325,16 @@ Apply the migrations to initialize the database.
|
|||
|
||||
Run the following command in the Package Manager Console (PMC):
|
||||
|
||||
```powershell
|
||||
Update-Database
|
||||
```
|
||||
`PM> Update-Database`
|
||||
|
||||
# [.NET Core CLI](#tab/netcore-cli)
|
||||
|
||||
Migrations are not necessary at this step when using SQLite.
|
||||
|
||||
[!INCLUDE [more information on the CLI for EF Core](~/includes/ef-cli.md)]
|
||||
|
||||
For LocalDB, run the following command:
|
||||
|
||||
```dotnetcli
|
||||
dotnet ef database update
|
||||
```
|
||||
|
@ -356,53 +353,75 @@ Run the app and register a user. Depending on your screen size, you might need t
|
|||
|
||||
Services are added in `ConfigureServices`. The typical pattern is to call all the `Add{Service}` methods, and then call all the `services.Configure{Service}` methods.
|
||||
|
||||
::: moniker-end
|
||||
|
||||
::: moniker range=">= aspnetcore-3.0 < aspnetcore-5.0"
|
||||
|
||||
[!code-csharp[](identity/sample/WebApp3/Startup.cs?name=snippet_configureservices&highlight=11-99)]
|
||||
|
||||
The preceding highlighted code configures Identity with default option values. Services are made available to the app through [dependency injection](xref:fundamentals/dependency-injection).
|
||||
|
||||
Identity is enabled by calling <xref:Microsoft.AspNetCore.Builder.AuthAppBuilderExtensions.UseAuthentication*>. `UseAuthentication` adds authentication [middleware](xref:fundamentals/middleware/index) to the request pipeline.
|
||||
|
||||
[!code-csharp[](identity/sample/WebApp3/Startup.cs?name=snippet_configure&highlight=19)]
|
||||
|
||||
::: moniker-end
|
||||
|
||||
::: moniker range=">= aspnetcore-5.0 < aspnetcore-6.0"
|
||||
|
||||
[!code-csharp[](identity/sample/WebApp5x/Startup.cs?name=snippet_configureservices&highlight=12-99)]
|
||||
|
||||
The preceding code configures Identity with default option values. Services are made available to the app through [dependency injection](xref:fundamentals/dependency-injection).
|
||||
|
||||
Identity is enabled by calling [UseAuthentication](/dotnet/api/microsoft.aspnetcore.builder.authappbuilderextensions.useauthentication#Microsoft_AspNetCore_Builder_AuthAppBuilderExtensions_UseAuthentication_Microsoft_AspNetCore_Builder_IApplicationBuilder_). `UseAuthentication` adds authentication [middleware](xref:fundamentals/middleware/index) to the request pipeline.
|
||||
|
||||
[!code-csharp[](identity/sample/WebApp1/Startup.cs?name=snippet_configure&highlight=18)]
|
||||
[!code-csharp[](identity/sample/WebApp5x/Startup.cs?name=snippet_configure&highlight=19)]
|
||||
|
||||
For more information, see the [IdentityOptions Class](/dotnet/api/microsoft.aspnetcore.identity.identityoptions) and [Application Startup](xref:fundamentals/startup).
|
||||
::: moniker-end
|
||||
|
||||
## Scaffold Register, Login, and LogOut
|
||||
::: moniker range=">= aspnetcore-3.0 < aspnetcore-6.0"
|
||||
|
||||
Follow the [Scaffold identity into a Razor project with authorization](xref:security/authentication/scaffold-identity#scaffold-identity-into-a-razor-project-with-authorization) instructions to generate the code shown in this section.
|
||||
The template-generated app doesn't use [authorization](xref:security/authorization/secure-data). `app.UseAuthorization` is included to ensure it's added in the correct order should the app add authorization. `UseRouting`, `UseAuthentication`, `UseAuthorization`, and `UseEndpoints` must be called in the order shown in the preceding code.
|
||||
|
||||
For more information on `IdentityOptions` and `Startup`, see <xref:Microsoft.AspNetCore.Identity.IdentityOptions> and [Application Startup](xref:fundamentals/startup).
|
||||
|
||||
## Scaffold Register, Login, LogOut, and RegisterConfirmation
|
||||
|
||||
# [Visual Studio](#tab/visual-studio)
|
||||
|
||||
Add the Register, Login, and LogOut files.
|
||||
Add the `Register`, `Login`, `LogOut`, and `RegisterConfirmation` files. Follow the [Scaffold identity into a Razor project with authorization](xref:security/authentication/scaffold-identity#scaffold-identity-into-a-razor-project-with-authorization) instructions to generate the code shown in this section.
|
||||
|
||||
# [.NET Core CLI](#tab/netcore-cli)
|
||||
|
||||
If you created the project with name **WebApp1**, run the following commands. Otherwise, use the correct namespace for the `ApplicationDbContext`:
|
||||
|
||||
When using SQLite, `--useSqLite` must be specified:
|
||||
If you created the project with name **WebApp1**, and you're not using SQLite, run the following commands. Otherwise, use the correct namespace for the `ApplicationDbContext`:
|
||||
|
||||
```dotnetcli
|
||||
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
|
||||
dotnet aspnet-codegenerator identity -dc WebApp1.Data.ApplicationDbContext --files "Account.Register;Account.Login;Account.Logout" --useSqLite
|
||||
dotnet aspnet-codegenerator identity -dc WebApp1.Data.ApplicationDbContext --files "Account.Register;Account.Login;Account.Logout;Account.RegisterConfirmation"
|
||||
```
|
||||
|
||||
With SQL Express, use the following commands:
|
||||
When using SQLite, append `--useSqlite` or `-sqlite`:
|
||||
|
||||
```dotnetcli
|
||||
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
|
||||
dotnet aspnet-codegenerator identity -dc WebApp1.Data.ApplicationDbContext --files "Account.Register;Account.Login;Account.Logout"
|
||||
dotnet aspnet-codegenerator identity -dc WebAppAuth.Data.ApplicationDbContext --files "Account.Register;Account.Login;Account.Logout;Account.RegisterConfirmation" --useSqlite
|
||||
```
|
||||
|
||||
PowerShell uses semicolon as a command separator. When using PowerShell, escape the semicolons in the file list or put the file list in double quotes, as the preceding example shows.
|
||||
|
||||
For more information on scaffolding Identity, see [Scaffold identity into a Razor project with authorization](xref:security/authentication/scaffold-identity#scaffold-identity-into-a-razor-project-with-authorization).
|
||||
|
||||
---
|
||||
|
||||
### Examine Register
|
||||
|
||||
When a user clicks the **Register** link, the `RegisterModel.OnPostAsync` action is invoked. The user is created by [CreateAsync](/dotnet/api/microsoft.aspnetcore.identity.usermanager-1.createasync#Microsoft_AspNetCore_Identity_UserManager_1_CreateAsync__0_System_String_) on the `_userManager` object:
|
||||
When a user clicks the **Register** button on the `Register` page, the `RegisterModel.OnPostAsync` action is invoked. The user is created by [CreateAsync](/dotnet/api/microsoft.aspnetcore.identity.usermanager-1.createasync#Microsoft_AspNetCore_Identity_UserManager_1_CreateAsync__0_System_String_) on the `_userManager` object:
|
||||
|
||||
[!code-csharp[](identity/sample/WebApp1/Areas/Identity/Pages/Account/Register.cshtml.cs?name=snippet&highlight=7)]
|
||||
[!code-csharp[](identity/sample/WebApp3/Areas/Identity/Pages/Account/Register.cshtml.cs?name=snippet&highlight=9)]
|
||||
|
||||
If the user was created successfully, the user is logged in by the call to `_signInManager.SignInAsync`.
|
||||
|
||||
**Note:** See [account confirmation](xref:security/authentication/accconfirm#prevent-login-at-registration) for steps to prevent immediate login at registration.
|
||||
<!-- .NET 5 fixes this, see
|
||||
https://github.com/dotnet/aspnetcore/blob/main/src/Identity/UI/src/Areas/Identity/Pages/V4/Account/RegisterConfirmation.cshtml.cs#L74-L77
|
||||
-->
|
||||
[!INCLUDE[](~/includes/disableVer.md)]
|
||||
|
||||
### Log in
|
||||
|
||||
|
@ -413,7 +432,7 @@ The Login form is displayed when:
|
|||
|
||||
When the form on the Login page is submitted, the `OnPostAsync` action is called. `PasswordSignInAsync` is called on the `_signInManager` object.
|
||||
|
||||
[!code-csharp[](identity/sample/WebApp1/Areas/Identity/Pages/Account/Login.cshtml.cs?name=snippet&highlight=10-11)]
|
||||
[!code-csharp[](identity/sample/WebApp3/Areas/Identity/Pages/Account/Login.cshtml.cs?name=snippet&highlight=10-11)]
|
||||
|
||||
For information on how to make authorization decisions, see <xref:security/authorization/introduction>.
|
||||
|
||||
|
@ -421,19 +440,21 @@ For information on how to make authorization decisions, see <xref:security/autho
|
|||
|
||||
The **Log out** link invokes the `LogoutModel.OnPost` action.
|
||||
|
||||
[!code-csharp[](identity/sample/WebApp1/Areas/Identity/Pages/Account/Logout.cshtml.cs)]
|
||||
[!code-csharp[](identity/sample/WebApp3/Areas/Identity/Pages/Account/Logout.cshtml.cs?highlight=36)]
|
||||
|
||||
In the preceding code, the code `return RedirectToPage();` needs to be a redirect so that the browser performs a new request and the identity for the user gets updated.
|
||||
|
||||
[SignOutAsync](/dotnet/api/microsoft.aspnetcore.identity.signinmanager-1.signoutasync#Microsoft_AspNetCore_Identity_SignInManager_1_SignOutAsync) clears the user's claims stored in a cookie.
|
||||
|
||||
Post is specified in the *Pages/Shared/_LoginPartial.cshtml*:
|
||||
|
||||
[!code-cshtml[](identity/sample/WebApp1/Pages/Shared/_LoginPartial.cshtml?highlight=16)]
|
||||
[!code-cshtml[](identity/sample/WebApp3/Pages/Shared/_LoginPartial.cshtml?highlight=15)]
|
||||
|
||||
## Test Identity
|
||||
|
||||
The default web project templates allow anonymous access to the home pages. To test Identity, add [`[Authorize]`](/dotnet/api/microsoft.aspnetcore.authorization.authorizeattribute) to the Privacy page.
|
||||
The default web project templates allow anonymous access to the home pages. To test Identity, add [`[Authorize]`](xref:Microsoft.AspNetCore.Authorization.AuthorizeAttribute):
|
||||
|
||||
[!code-csharp[](identity/sample/WebApp1/Pages/Privacy.cshtml.cs?highlight=7)]
|
||||
[!code-csharp[](identity/sample/WebApp3/Pages/Privacy.cshtml.cs?highlight=7)]
|
||||
|
||||
If you are signed in, sign out. Run the app and select the **Privacy** link. You are redirected to the login page.
|
||||
|
||||
|
@ -446,7 +467,7 @@ To explore Identity in more detail:
|
|||
|
||||
## Identity Components
|
||||
|
||||
All the Identity dependent NuGet packages are included in the [Microsoft.AspNetCore.App metapackage](xref:fundamentals/metapackage-app).
|
||||
All the Identity-dependent NuGet packages are included in the [ASP.NET Core shared framework](xref:aspnetcore-3.0#use-the-aspnet-core-shared-framework).
|
||||
|
||||
The primary package for Identity is [Microsoft.AspNetCore.Identity](https://www.nuget.org/packages/Microsoft.AspNetCore.Identity/). This package contains the core set of interfaces for ASP.NET Core Identity, and is included by `Microsoft.AspNetCore.Identity.EntityFrameworkCore`.
|
||||
|
||||
|
@ -458,8 +479,37 @@ For more information and guidance on migrating your existing Identity store, see
|
|||
|
||||
See [Configuration](#pw) for a sample that sets the minimum password requirements.
|
||||
|
||||
## AddDefaultIdentity and AddIdentity
|
||||
|
||||
<xref:Microsoft.Extensions.DependencyInjection.IdentityServiceCollectionUIExtensions.AddDefaultIdentity*> was introduced in ASP.NET Core 2.1. Calling `AddDefaultIdentity` is similar to calling the following:
|
||||
|
||||
* <xref:Microsoft.Extensions.DependencyInjection.IdentityServiceCollectionExtensions.AddIdentity*>
|
||||
* <xref:Microsoft.AspNetCore.Identity.IdentityBuilderUIExtensions.AddDefaultUI*>
|
||||
* <xref:Microsoft.AspNetCore.Identity.IdentityBuilderExtensions.AddDefaultTokenProviders*>
|
||||
|
||||
See [AddDefaultIdentity source](https://github.com/dotnet/AspNetCore/blob/release/3.1/src/Identity/UI/src/IdentityServiceCollectionUIExtensions.cs#L47-L63) for more information.
|
||||
|
||||
## Prevent publish of static Identity assets
|
||||
|
||||
To prevent publishing static Identity assets (stylesheets and JavaScript files for Identity UI) to the web root, add the following `ResolveStaticWebAssetsInputsDependsOn` property and `RemoveIdentityAssets` target to the app's project file:
|
||||
|
||||
```xml
|
||||
<PropertyGroup>
|
||||
<ResolveStaticWebAssetsInputsDependsOn>RemoveIdentityAssets</ResolveStaticWebAssetsInputsDependsOn>
|
||||
</PropertyGroup>
|
||||
|
||||
<Target Name="RemoveIdentityAssets">
|
||||
<ItemGroup>
|
||||
<StaticWebAsset Remove="@(StaticWebAsset)" Condition="%(SourceId) == 'Microsoft.AspNetCore.Identity.UI'" />
|
||||
</ItemGroup>
|
||||
</Target>
|
||||
```
|
||||
|
||||
<a name="next"></a>
|
||||
|
||||
## Next Steps
|
||||
|
||||
* [ASP.NET Core Identity source code](https://github.com/dotnet/aspnetcore/tree/main/src/Identity)
|
||||
* See [this GitHub issue](https://github.com/dotnet/AspNetCore.Docs/issues/5131) for information on configuring Identity using SQLite.
|
||||
* [Configure Identity](xref:security/authentication/identity-configuration)
|
||||
* <xref:security/authorization/secure-data>
|
||||
|
|
Loading…
Reference in New Issue