Wednesday 10 February 2021

Part 26: Authentication using Identity in Asp.Net core 3.1

#5 minutes of reading
Lets revert back migration created in article 25, open database and delete entry from Database table highlighted below.
Delete below highlighted table from database.
Delete highlighted file from migration folder.
  Install Nuget package for Identity. Identity is an API provided by Microsoft which provide Authentication and Authorization related functionality including Roles, Claims and Policy.
Update database with identity table using packages manager console commands shown below.
Database table gets generated in database.
Install package for Identity from Nuget.
  Update Startup.cs file for registering Identity services shown below. IdentityUser and IdentityRole class which is provided by Identity API as model and AddIdentity method highlighted below is configuring all service for IdentityUser and Role class.
Inject UserManager and SignInmanager in Account controller to access default methods from these classes. Classes available inside Microsoft.AspNetCore.Identity namespace.
FindByNameAsync communicate with IdentityUser table and get User details if found in IdentityUser table. PasswordSignInAsyn method is used to SignIn into application once user authenticated.

For User Model, I have used Required attribute befor each property except Id of of User Model which is nothing but validating property shouldn't be empty while submitting data from Register/Login view. I will dicuss more on Validation later in future article.
Add Register View for Register action method.
Run application and navigate to register view to register user.
Registration successful, you will get redirected to Mobile Brand page.
For User registration and Login, no action link available on header section,in next article will show you how to add Action link for Register and Login on header section.  

1 Comments:

At 10 February 2021 at 17:27 , Blogger Nihal Ahmed said...

Nice explained

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home