At first, we need to configure the Controllers/HomeController.cs file.
In the headers, add the following lines:
using Microsoft.AspNetCore.Authentication; // to add authentication
using Microsoft.AspNetCore.Authentication.Cookies; // to add cookie
using Microsoft.AspNetCore.Authorization; // to add authorization
using System.Security.Claims; // to add claims -> claims are the authorization details
Claims are the authorization details.Let’s say that the Privacy page is only accessible to authenticated users. So, in the class HomeController, after the Index method, let’s add the [Authorize] attribute to the Privacy method.
[Authorize]
public IActionResult Privacy()
{
    return View();
}
Now, let’s add the login method to the HomeController class as the authentication form and the logic to authenticate the user is needed.
/**
* This is the get method for login
* It will take return url and send it to the view
*/
[HttpGet]
public IActionResult Login(string ReturnUrl)
{
    // take return url to view for this use ViewData
    ViewData["returnURL"] = ReturnUrl;
    return View();
}
Now, let’s add the post method for the login. This will take the username and password and return to the return URL.
/**
* This is the post method for login
* It will take username and password and return to the return url
*/
[HttpPost]
public IActionResult Login(string username, string password, string ReturnUrl)
{
    if (username == "ram" && password == "ram")
    {
        // add authroization
        // claim: authorization detail
        // identity: which mechanism to be used for authorization:cookie
        // principal: who is authorized
        List<Claim> claims = new List<Claim>();
        claims.Add(new Claim(ClaimTypes.NameIdentifier, username));
        claims.Add(new Claim(ClaimTypes.Name, username));
        // identity->claims
        ClaimsIdentity identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
        // principal->identity
        ClaimsPrincipal principal = new ClaimsPrincipal(identity);
        // executing->sign in
        HttpContext.SignInAsync(principal); //execute
        return Redirect(ReturnUrl);
    }
    return View();
}
In this method:
Now, create a Login.cshtml file in the Views/Home folder for the authentication form.
@* create login form and also extract return url *@ @{ ViewData["Title"]
="Login"; } @{ string retUrl = ViewData["returnURL"] as string; }
<form
  method="post"
  action="Login?ReturnUrl=@System.Net.WebUtility.UrlEncode(retUrl)"
>
  <label>Username</label>
  <input type="text" name="username" /><br />
  <label>Password</label>
  <input type="text" name="password" /><br />
  <input type="submit" name="submit" value="login" />
</form>
action="Login?ReturnUrl=@System.Net.WebUtility.UrlEncode(retUrl)".Login method.UrlEncode method will encode the URL. This is necessary because the URL may contain special characters that may break the URL.Login method.Then, in Program.cs:
// to add cookie authentication
using Microsoft.AspNetCore.Authentication.Cookies;
// adding authentication through cookie
// after adding cookie, login path must be /Home/Login
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options => options.LoginPath = "/Home/Login");
/Home/Login.The roles are the permissions that the user has. We can add roles to the user and check if the user has the necessary roles to access the resources.
The process of adding roles is similar to adding claims. We need to add the roles to the claims and then add the claims to the identity.
Let’s say that we have a role called Student. We can add this role to the claims. In the Login method in the HomeController class, add the role to the claims.
if (username == "ram" && password == "ram")
{
    // add authroization
    // claim: authorization detail
    // identity: which mechanism to be used for authorization:cookie
    // principal: who is authorized
    List<Claim> claims = new List<Claim>();
    claims.Add(new Claim(ClaimTypes.NameIdentifier, username));
    claims.Add(new Claim(ClaimTypes.Name, username));
    /** adding role **/
    claims.Add(new Claim(ClaimTypes.Role, "Student")); // adding role
    // identity->claims
    ClaimsIdentity identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
    // principal->identity
    ClaimsPrincipal principal = new ClaimsPrincipal(identity);
    // executing->sign in
    HttpContext.SignInAsync(principal); //execute
    return Redirect(ReturnUrl);
}
Student to the claims.Now, let’s say that the Dashboard page is only accessible to the users with the role Student. We can add the [Authorize(Roles = "Student")] attribute to the Dashboard method in the HomeController class.
[Authorize(Roles = "Student")]
public IActionResult Dashboard()
{
    return View();
}
Student.Dashboard.cshtml file in the Views/Home folder.Student.