Skip to main content

Posts

Showing posts with the label Web API

Apply JWT Access Tokens and Refresh Tokens in ASP .NET Core Web API

 To apply JWT (JSON Web Token) access tokens and refresh tokens in an ASP .NET Core Web API, you can follow these steps: Install the necessary NuGet packages: dotnet add package Microsoft.AspNetCore.Authentication.JwtBearer dotnet add package Microsoft.AspNetCore.Authorization In the Startup.cs file, configure the JWT authentication scheme in the ConfigureServices method: services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true, ValidIssuer = Configuration["Jwt:Issuer"], ValidAudience = Configuration["Jwt:Audience"], IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:Key"])) }; }); In the Config...