How to enable authorization in ASP.NET Core Archives - Tech Insights Unveiling Tomorrow's Tech Today, Where Innovation Meets Insight Wed, 18 Jan 2023 10:56:13 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.2 https://i0.wp.com/reactconf.org/wp-content/uploads/2023/11/cropped-reactconf.png?fit=32%2C32&ssl=1 How to enable authorization in ASP.NET Core Archives - Tech Insights 32 32 230003556 ASP.Net Core Authentication and Authorization Basics in .Net Core 6.0 https://reactconf.org/how-to-implement-authentication-and-authorization-in-asp-net-core/ https://reactconf.org/how-to-implement-authentication-and-authorization-in-asp-net-core/#respond Wed, 18 Jan 2023 10:56:13 +0000 https://labpys.com/?p=743 In this article, we will discuss the ASP.Net Core Authentication and Authorization Basics in .Net Core 6.0. Prerequisites Previous Article you can also check Create ASP.Net Core Web API Create …

The post ASP.Net Core Authentication and Authorization Basics in .Net Core 6.0 appeared first on Tech Insights.

]]>
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>

    The post ASP.Net Core Authentication and Authorization Basics in .Net Core 6.0 appeared first on Tech Insights.

    ]]>
    https://reactconf.org/how-to-implement-authentication-and-authorization-in-asp-net-core/feed/ 0 743