You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
By [Rick Anderson](https://twitter.com/RickAndMSFT)
15
16
@@ -106,22 +107,6 @@ Run the app and register a user. Depending on your screen size, you might need t
106
107
107
108
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.
The preceding highlighted code configures Identity with default option values. Services are made available to the app through [dependency injection](xref:fundamentals/dependency-injection).
116
-
117
-
Identity is enabled by calling <xref:Microsoft.AspNetCore.Builder.AuthAppBuilderExtensions.UseAuthentication*>. `UseAuthentication` adds authentication [middleware](xref:fundamentals/middleware/index) to the request pipeline.
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
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.
138
119
139
120
For more information on `IdentityOptions` and `Startup`, see <xref:Microsoft.AspNetCore.Identity.IdentityOptions> and [Application Startup](xref:fundamentals/startup).
@@ -275,29 +256,35 @@ To prevent publishing static Identity assets (stylesheets and JavaScript files f
By [Rick Anderson](https://twitter.com/RickAndMSFT)
281
262
282
-
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).
263
+
ASP.NET Core Identity:
283
264
284
-
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.
265
+
* Is an API that supports user interface (UI) login functionality.
266
+
* Manages users, passwords, profile data, roles, claims, tokens, email confirmation, and more.
285
267
286
-
[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)).
268
+
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).
287
269
288
-
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.
270
+
[!INCLUDE[](~/includes/requireAuth.md)]
289
271
290
-
<aname="adi"></a>
272
+
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.
291
273
292
-
## AddDefaultIdentity and AddIdentity
274
+
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.
293
275
294
-
<xref:Microsoft.Extensions.DependencyInjection.IdentityServiceCollectionUIExtensions.AddDefaultIdentity*> was introduced in ASP.NET Core 2.1. Calling `AddDefaultIdentity` is similar to calling the following:
276
+
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:
299
279
300
-
See [AddDefaultIdentity source](https://github.com/dotnet/AspNetCore/blob/release/2.1/src/Identity/UI/src/IdentityServiceCollectionUIExtensions.cs#L47-L63) for more information.
280
+
* An evolution of the Azure Active Directory (Azure AD) developer platform.
281
+
* Unrelated to ASP.NET Core Identity.
282
+
283
+
[!INCLUDE[](~/includes/IdentityServer4.md)]
284
+
285
+
[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)).
286
+
287
+
<aname="adi"></a>
301
288
302
289
## Create a Web app with authentication
303
290
@@ -316,6 +303,12 @@ Create an ASP.NET Core Web Application project with Individual User Accounts.
316
303
dotnet new webapp --auth Individual -o WebApp1
317
304
```
318
305
306
+
The preceding command creates a Razor web app using SQLite. To create the web app with LocalDB, run the following command:
307
+
308
+
```dotnetcli
309
+
dotnet new webapp --auth Individual -uld -o WebApp1
310
+
```
311
+
319
312
---
320
313
321
314
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.
332
325
333
326
Run the following command in the Package Manager Console (PMC):
334
327
335
-
```powershell
336
-
Update-Database
337
-
```
328
+
`PM> Update-Database`
338
329
339
330
# [.NET Core CLI](#tab/netcore-cli)
340
331
332
+
Migrations are not necessary at this step when using SQLite.
333
+
334
+
[!INCLUDE [more information on the CLI for EF Core](~/includes/ef-cli.md)]
335
+
336
+
For LocalDB, run the following command:
337
+
341
338
```dotnetcli
342
339
dotnet ef database update
343
340
```
@@ -356,53 +353,75 @@ Run the app and register a user. Depending on your screen size, you might need t
356
353
357
354
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.
The preceding highlighted code configures Identity with default option values. Services are made available to the app through [dependency injection](xref:fundamentals/dependency-injection).
363
+
364
+
Identity is enabled by calling <xref:Microsoft.AspNetCore.Builder.AuthAppBuilderExtensions.UseAuthentication*>. `UseAuthentication` adds authentication [middleware](xref:fundamentals/middleware/index) to the request pipeline.
The preceding code configures Identity with default option values. Services are made available to the app through [dependency injection](xref:fundamentals/dependency-injection).
360
375
361
376
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.
For more information, see the [IdentityOptions Class](/dotnet/api/microsoft.aspnetcore.identity.identityoptions) and [Application Startup](xref:fundamentals/startup).
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.
384
+
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.
385
+
386
+
For more information on `IdentityOptions` and `Startup`, see <xref:Microsoft.AspNetCore.Identity.IdentityOptions> and [Application Startup](xref:fundamentals/startup).
387
+
388
+
## Scaffold Register, Login, LogOut, and RegisterConfirmation
370
389
371
390
# [Visual Studio](#tab/visual-studio)
372
391
373
-
Add the Register, Login, and LogOut files.
392
+
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.
374
393
375
394
# [.NET Core CLI](#tab/netcore-cli)
376
395
377
-
If you created the project with name **WebApp1**, run the following commands. Otherwise, use the correct namespace for the `ApplicationDbContext`:
378
-
379
-
When using SQLite, `--useSqLite` must be specified:
396
+
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`:
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.
394
410
411
+
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).
412
+
395
413
---
396
414
397
415
### Examine Register
398
416
399
-
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:
402
418
403
-
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.
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.
425
446
426
447
[SignOutAsync](/dotnet/api/microsoft.aspnetcore.identity.signinmanager-1.signoutasync#Microsoft_AspNetCore_Identity_SignInManager_1_SignOutAsync) clears the user's claims stored in a cookie.
427
448
428
449
Post is specified in the *Pages/Shared/_LoginPartial.cshtml*:
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.
455
+
The default web project templates allow anonymous access to the home pages. To test Identity, add [`[Authorize]`](xref:Microsoft.AspNetCore.Authorization.AuthorizeAttribute):
If you are signed in, sign out. Run the app and select the **Privacy** link. You are redirected to the login page.
439
460
@@ -446,7 +467,7 @@ To explore Identity in more detail:
446
467
447
468
## Identity Components
448
469
449
-
All the Identitydependent NuGet packages are included in the [Microsoft.AspNetCore.App metapackage](xref:fundamentals/metapackage-app).
470
+
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).
450
471
451
472
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`.
452
473
@@ -458,8 +479,37 @@ For more information and guidance on migrating your existing Identity store, see
458
479
459
480
See [Configuration](#pw) for a sample that sets the minimum password requirements.
460
481
482
+
## AddDefaultIdentity and AddIdentity
483
+
484
+
<xref:Microsoft.Extensions.DependencyInjection.IdentityServiceCollectionUIExtensions.AddDefaultIdentity*> was introduced in ASP.NET Core 2.1. Calling `AddDefaultIdentity` is similar to calling the following:
See [AddDefaultIdentity source](https://github.com/dotnet/AspNetCore/blob/release/3.1/src/Identity/UI/src/IdentityServiceCollectionUIExtensions.cs#L47-L63) for more information.
491
+
492
+
## Prevent publish of static Identity assets
493
+
494
+
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:
0 commit comments