ASP.Net Core Authentication and Authorization Basics in .Net Core 6.0

In this article, we will discuss the ASP.Net Core Authentication and Authorization Basics in .Net Core 6.0.

Prerequisites

  • Download and install .Net Core 6.0 SDK from here
  • Download and Install Microsoft Visual Studio 2022 from here

Previous Article you can also check Create ASP.Net Core Web API

Create ASP.NET Core Web App

  • Open visual studio and click the Create New Project Option
  • Select the Blank Template
  • Enter the name of the Project

Select Framework and Click on Create

    Create Controller

    Create the Home Controller

    using Microsoft.AspNetCore.Authentication;
    using Microsoft.AspNetCore.Authorization;
    using Microsoft.AspNetCore.Mvc;
    using System.Security.Claims;
    
    namespace App_Auth_Authorization.Controllers
    {
        public class HomeController : Controller
        {
            public IActionResult Index()
            {
                return View();
            }
    
            [Authorize]
            public IActionResult Employee()
            {
                return View();
            }
     public async Task<IActionResult> Authenticate()
            {
                var Auth_Claims = new List<Claim>
                {
                    new Claim(ClaimTypes.Name,"labpys.com"),
                    new Claim(ClaimTypes.Name,"Example@labpys.com"),               
    
                };
    
                var license = new List<Claim>() {
    
                    new Claim(ClaimTypes.Name, "Admin"),
                    new Claim("UniqueIdentity", "B+"),
    
                };
    
    
                var AuthIdenity = new ClaimsIdentity(Auth_Claims, "Auth_Tutorial");
                var LicenseIdentity = new ClaimsIdentity(license, "Auth_Tutorial");
                var userPrincipal = new ClaimsPrincipal( new[] { AuthIdenity, LicenseIdentity });
                await HttpContext.SignInAsync(userPrincipal);
    
                return Redirect(nameof(Index));
            }
    
            
        }
    }
    

    Authorization – To make ASP.NET Core Authorized Attribute work, you must include the relevant middleware in program class.

    var builder = WebApplication.CreateBuilder(args);
    
    var services = builder.Services;    
    
    services.AddControllersWithViews();
    
    services.AddAuthentication("CookieAuth")
        .AddCookie("CookieAuth", config =>
        {
            config.Cookie.Name = "Auth_Tutorial";
            config.LoginPath = "/Home/Authenticate";
    
        });
    services.AddMvc();
    
    var app = builder.Build();
    
    app.UseAuthentication();
    app.UseRouting();
    app.UseAuthorization();
    
    
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapDefaultControllerRoute();
    });
    app.Run();
    

    Customize View

    Index.cshtml

     
    @{
    }
    
    <h1>Layout Page</h1>
    

    Employee.cshtml

     
    @{
    }
    
    <h1>Employee Detail</h1>
    
    0 0 votes
    Article Rating
    Subscribe
    Notify of
    guest

    0 Comments
    Inline Feedbacks
    View all comments