summaryrefslogtreecommitdiff
path: root/Program.cs
diff options
context:
space:
mode:
authorHombreLaser <sebastian-440@live.com>2022-11-17 20:16:10 -0600
committerHombreLaser <sebastian-440@live.com>2022-11-17 20:16:10 -0600
commit603fd6bab11275eeca6f83479edf496b07bb0b74 (patch)
tree497b36ca28254d098cce5b50dc1c5ea23ad9f23e /Program.cs
parentfb3e3ed608780e2db3ba5b3cef93e098d14af43d (diff)
Corregida configuraciĆ³n
Diffstat (limited to 'Program.cs')
-rw-r--r--Program.cs75
1 files changed, 71 insertions, 4 deletions
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<ApplicationDbContext>(opt => opt.UseNpgsql(builder.Configuration.GetConnectionString("ApplicationDbContext")));
-builder.Services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();
+builder.Services.AddIdentity<UserAccount, IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>().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<IdentityOptions>(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();