IdentityServiceRegistration Creado
Este archivo contiene la configuración de la validación de tokens
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
using CleanArchitecture.Application.Contracts.Identity;
|
||||
using CleanArchitecture.Application.Models.Identity;
|
||||
using CleanArchitecture.Identity.Models;
|
||||
using CleanArchitecture.Identity.Services;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System.Text;
|
||||
|
||||
namespace CleanArchitecture.Identity
|
||||
{
|
||||
public static class IdentityServiceRegistration
|
||||
{
|
||||
public static IServiceCollection ConfigureIdentityServices(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.Configure<JwtSettings>(configuration.GetSection("JwtSettings"));
|
||||
|
||||
var dbConnectionString = configuration.GetConnectionString("IdentityConnectionString");
|
||||
services.AddDbContext<CleanArchitectureIdentityDbContext>(options =>
|
||||
options.UseMySql(dbConnectionString, ServerVersion.AutoDetect(dbConnectionString),
|
||||
b => b.MigrationsAssembly(typeof(CleanArchitectureIdentityDbContext).Assembly.FullName)));
|
||||
|
||||
services.AddIdentity<ApplicationUser, IdentityRole>().AddEntityFrameworkStores<CleanArchitectureIdentityDbContext>().AddDefaultTokenProviders();
|
||||
|
||||
services.AddTransient<IAuthService, AuthService>();
|
||||
|
||||
services.AddAuthentication(options =>
|
||||
{
|
||||
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
}).AddJwtBearer(options =>
|
||||
{
|
||||
options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters {
|
||||
ValidateIssuerSigningKey = true,
|
||||
ValidateIssuer = true,
|
||||
ValidateAudience = true,
|
||||
ValidateLifetime = true,
|
||||
ClockSkew = TimeSpan.Zero,
|
||||
ValidIssuer = configuration["JwtSettings:Issuer"],
|
||||
ValidAudience = configuration["JwtSettings:Audience"],
|
||||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["JwtSettings:Key"]!))
|
||||
};
|
||||
});
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user