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.entityframeworkcoredotnet add package microsoft.aspnetcore.identity.uiUpdate the
DbContextusing 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.cshtmlAccount>_ViewImports.cshtmlLogin.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.csapp.UseAuthorization();Decorate Controller and/or Action(s) with
Authorizeattribute[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() { } }