From Pluralsight/ASP.NET Core 6 Fundamentals
ASP.NET Core Identity
- A&A system that supports UI login functionality
- Not for APIs
- Supports external service providers
- Supports MVC & Razor Pages, scaffolding, SQL Server
- Includes a Razor Class Library for identity-enabled Views that typically need to be included in a web app
Important Classes
UserManager<IdentityUser>
Manages all interaction (CRUD operations) with user objects in the datastore.
SignInManager<IdentityUser>
User authentication and related actions; defines methods such as PasswordSignInAsync
, ConfirmEmailAsync
, SignOutAsync
, etc.
Adding ASP.NET Core Identity to an App
Configuring
Add required packages
dotnet add package microsoft.aspnetcore.identity.entityframeworkcore
dotnet add package microsoft.aspnetcore.identity.ui
Update the
DbContext
using Microsoft.AspNetCore.Identity.EntityFrameworkCore; public class BethanysPieShopDbContext : IdentityDbContext // must inherit from this base class
- Add identity services to DI container
Program.cs
builder.Services.AddDefaultIdentity<IdentityUser>(options => // IdentityUser is a built-in type to represent a user { // registration will only succeed if these conditions are met: options.Password.RequireDigit = true; options.Password.RequiredLength = 8; options.Password.RequireNonAlphanumeric = true; options.User.RequireUniqueEmail = true; }) .AddEntityFrameworkStores<BethanysPieShopDbContext>(); // use EF for identity data
- Add identity services to DI container
Add authentication middlewareZ
app.UseAuthentication();
Run a build
Create a migration Creates an ASP.NET
Roles
,Users
,RoleClaims
,UserClaims
,UserLogins
, and other tables: pmc >add-migration IdentityAdded
pmc >update-database
Adding Authentication
Two techniques: manual approach or scaffolding
Scaffolding
Right-click project > Add > New Scaffolded Item… > Identity > Add > check Override all files OR check specific items > Data context class: SomeDbContext > Add This will, among other things, make changes to
Program.cs
.
Adds:Areas
>Identity
>Pages
>_ValidationScriptsPartial.cshtml
,_ViewImports.cshtml
,_ViewStart.cshtml
Account
>_ViewImports.cshtml
Login.cshtml
— the Login UIRegister.cshtml
— the UI to register a new accountUpdate layout to account for scripts that scaffolding added:
/Views/Shared/_Layout.cshtml
Below@RenderBody()
@RenderSection("Scripts", required: false)
Update layout to add the login partial:
/Views/Shared/_Layout.cshtml
<partial name="_LoginPartial" />
Adding Authorization
Add authorization middleware:
Program.cs
app.UseAuthorization();
Decorate Controller and/or Action(s) with
Authorize
attribute[Authorize]
attribute
- Can be added to Controller to a Controller’s Action - Confirms user is logged in before proceeding[Authorize(Roles = "RoleGroup")]
- Checks if user is logged in AND if user is part of RoleGroup before proceedingExample
[Authorize] public class OrderController : Controller { [Authorize] // requires user to be logged in to use this method public IActionResult Checkout() { } }