From 603fd6bab11275eeca6f83479edf496b07bb0b74 Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Thu, 17 Nov 2022 20:16:10 -0600 Subject: Corregida configuraciĆ³n MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Program.cs | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 71 insertions(+), 4 deletions(-) (limited to 'Program.cs') diff --git a/Program.cs b/Program.cs index 32c8b60..22bf80e 100644 --- a/Program.cs +++ b/Program.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.IdentityModel.Tokens; +using Microsoft.OpenApi.Models; using System.IdentityModel.Tokens.Jwt; using System.Text.Json.Serialization; using System.Text; @@ -12,22 +13,88 @@ var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers().AddNewtonsoftJson(); -// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); builder.Services.AddDbContext(opt => opt.UseNpgsql(builder.Configuration.GetConnectionString("ApplicationDbContext"))); -builder.Services.AddIdentity().AddEntityFrameworkStores().AddDefaultTokenProviders(); +builder.Services.AddIdentity().AddEntityFrameworkStores().AddDefaultTokenProviders(); +// Automapper configuration. +builder.Services.AddAutoMapper(typeof(Program)); + +// Swagger configuration. +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(c => { + c.SwaggerDoc("v1", new OpenApiInfo { Title = "IdentityAPI", Version = "v1" }); + + c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme { + Name = "Authorization", + Type = SecuritySchemeType.ApiKey, + Scheme = "Bearer", + BearerFormat = "JWT", + In = ParameterLocation.Header } + ); + + c.AddSecurityRequirement(new OpenApiSecurityRequirement { + { + new OpenApiSecurityScheme { + Reference = new OpenApiReference { + Type = ReferenceType.SecurityScheme, + Id = "Bearer" + } + }, + new String[]{} + } + } + ); + } + ); +// End of swagger configuration. + +// Authentication configuration. builder.Services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(options => options.TokenValidationParameters = new TokenValidationParameters { - ValidateIssuer = false, + ValidateIssuer = false, ValidateAudience = false, ValidateLifetime = true, ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"])), - ClockSkew = TimeSpan.Zero}); + ClockSkew = TimeSpan.Zero } + ); +// End of authentication configuration. + +// Identity configuration. +builder.Services.Configure(options => +{ + // Password settings. + options.Password.RequireDigit = false; + options.Password.RequireLowercase = false; + options.Password.RequireNonAlphanumeric = false; + options.Password.RequireUppercase = false; + options.Password.RequiredLength = 6; + options.Password.RequiredUniqueChars = 0; + + // Lockout settings. + options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5); + options.Lockout.MaxFailedAccessAttempts = 5; + options.Lockout.AllowedForNewUsers = true; + + // User settings. + options.User.AllowedUserNameCharacters = + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+"; + options.User.RequireUniqueEmail = false; +}); +// End of identity configuration. + +// CORS configuration +builder.Services.AddCors(options => { + options.AddDefaultPolicy(builder => { + builder.WithOrigins("https://apirequest.io").AllowAnyMethod().AllowAnyHeader(); + } + ); +}); +// End of cors configuration. var app = builder.Build(); -- cgit v1.2.3