How to carry out identity authentication in minimal APIs in ASP.NET Core


Very Little APIs in ASP.NET Core allow us to develop lightweight APIs with very little reliances. However, often we will still require authentication and permission in our minimal APIs. There are several methods to attain this in ASP.NET Core including standard authentication, token-based authentication, and identity-based authentication.We gone over executing fundamental authentication in minimal APIs here, and JWT token-based authentication in minimal APIs here. In this post we’ll take a look at how we can carry out identity-based authentication for minimal APIs in ASP.NET Core.To utilize the code examples offered in this short article, you need to have Visual Studio 2022 installed in your system. If you don’t currently have a copy, you can download Visual Studio 2022 here.

Develop an ASP.NET Core Web API job in Visual Studio 2022 To produce an ASP.NET Core Web API job in Visual Studio 2022, follow the steps outlined listed below. Release the Visual Studio 2022 IDE.

Click on “Develop new job.” In the” Produce brand-new task”window, choose “ASP.NET Core Web API “from the list of design templates showed. Click Next. In the “Configure your brand-new task

  • “window, define the name and place for the
  • new project. Additionally inspect the”
  • Location option and job in the very same directory site”check box, depending upon your preferences. Click Next.
  • In the “Additional
  • Info”window revealed next, select”. WEB 8.0( Long Term Support) “as the framework version and uncheck the check box that states “Usage controllers,”as we’ll be using minimal APIs in this job.
  • Elsewhere in
  • the “Additional Details” window, leave the”Authentication Type” set to” None”(the default )and make sure the check boxes “Enable Open API Assistance,””Set up for HTTPS,” and” Enable Docker”stay unchecked. We will not be using any of those features here. Click Create. We’ll utilize this ASP.NET Core Web API job to deal with the code examples given up the areas below.Identity management in ASP.NET Core ASP.NET Core consists of a powerful function referred to as identity management that has actually been boosted in. NET 8.
  • The built-in Identity

    framework in ASP.NET Core provides the needed middleware to implement authentication, user management, and

    role-based authorization, thus making it simpler to implement robust and safe authentication mechanisms in your application. ASP.NET Core’s Identity structure is extensible and adjustable with assistance for the following key features: Authentication and permission User management Functions management Password hashing Token-based authentication Claims-based authentication Create a minimal API in ASP.NET Core In the Web API project we developed above, change the generated code with the following code to create a fundamental minimal API. var home builder=WebApplication.CreateBuilder(args); var app=builder.Build(); app.MapGet (“/ helloworld “,() =>”Hey there, World! “); app.Run (); When you carry out the application, the text”Hello World!”

  • will be shown in your web browser. We’ll use
  • this endpoint later on in this article.Install NuGet packages To add assistance for the integrated Identity framework in ASP.NET Core, select the project in the Solution Explorer window, then right-click and choose” Handle NuGet Plans.”In the NuGet Package Manager window, search for the Microsoft.AspNetCore.Identity.EntityFrameworkCore, Microsoft.EntityFrameworkCore.SqlServer, and Microsoft.EntityFrameworkCore.Design packages and install them.Alternatively, you can install the plans through the NuGet Plan Manager console by going into the commands listed below.PM > Install-Package Microsoft.AspNetCore.Identity.EntityFrameworkCore PM

    > Install-Package Microsoft.EntityFrameworkCore.SqlServer PM > Install-Package Microsoft.EntityFrameworkCore.Design Develop a brand-new DbContext in EF Core We’ll be using Entity Framework Core in this example. The DbContext is an integral part of EF Core that represents a connection session with the database. Next, develop a customized DbContext class by extending the IdentityDbContext class as shown in the code bit given listed below. public class CustomDbContext(DbContextOptions options): IdentityDbContext( options) You should sign up the custom DbContext class by consisting of the following line of code in the Program.cs file.builder.Services.AddDbContext (); Enable authentication and permission in ASP.NET Core Authentication is the process of determining who the user is and confirming the user’s identity. You

    can enable authentication in a minimal API in ASP.NET Core by utilizing the AddAuthentication()method as displayed in the code bit provided below.var builder =WebApplication.CreateBuilder (args); builder.Services.AddAuthentication(); We utilize permission to restrict access to particular resources in an application. You can enable permission in your very little API by using the following code.builder.Services.AddAuthorization(); The AddAuthorizationtechnique is used to register authorization

    services with the services container so that you can define rules for enabling or disabling access to resources of the

    application if needed. Configureservices and API endpoints in ASP.NET Core The next thing we need to do is

    configure the identity and EF Core services and the API endpoints. To do this, consist of the code listing given below in the Program.cs file.using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; builder.Services.AddDbContext();

    builder.Services.AddAuthorization (); builder.Services.AddIdentityApiEndpoints(). AddEntityFrameworkStores(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen();

    var app=builder.Build (); app.MapIdentityApi(); The AddIdentityApiEndpoints ()approach in the preceding code bit includes the essential controllers and services for authentication and authorization( login, logout, registration, and so on ). Keep in mind that this is a new method (introduced in.NET 8)used to set up Identity integration in an application. The AddIdentityApiEndpoints() approach accepts a circumstances of type IdentityUser as a criterion, which is utilized to define the kind of user.You can use the following piece of code to include authorization for the/ helloworld“/ helloworld”,()=>”Hello World!”). RequireAuthorization(); Total source of the Program.cs file The complete source code of the Program.cs file is provided listed below for your reference.using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; var home builder=WebApplication.CreateBuilder(args);// Add services to the container. builder.Services.AddDbContext();

    builder.Services.AddAuthorization(); builder.Services.AddIdentityApiEndpoints(). AddEntityFrameworkStores(); builder.Services.AddEndpointsApiExplorer (); builder.Services.AddSwaggerGen(); var app=builder.Build (); app.MapIdentityApi ();// Set up the HTTP demand pipeline. app.MapGet(“/ helloworld “,()=>”Hey there World!”). RequireAuthorization(); app.UseSwagger(); app.UseSwaggerUI(c= > swagger. json”,”v1″);); app.Run (); public class CustomDbContext(DbContextOptions alternatives): IdentityDbContext (choices)

    The incorporated identity management function in ASP.NET Core is both effective and simple to utilize. The enhancements in.NET 8 have actually made Identity a lot more robust and versatile with an enhanced Identity API, which allows you to implement identity-based authentication and permission more quickly and efficiently with less code. Copyright © 2024 IDG Communications, Inc. Source

  • Leave a Reply

    Your email address will not be published. Required fields are marked *