I’m trying to use all the new features in .net core 8.
I’m using this code
using IspH2H.Engine;
using IspH2H.Engine.Entities;
using IspH2H.WebAPI.Security;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(
JwtBearerDefaults.AuthenticationScheme,
options => builder.Configuration.Bind("JwtSettings", options));
builder.Services.Configure(builder.Configuration.GetSection(SmtpEmailSenderConfiguration.SmtpEmailSender));
builder.Services.AddSingleton, SmtpEmailSender>();
builder.Services.AddDbContext(opt =>
opt.UseSqlServer(builder.Configuration.GetConnectionString("Default")));
builder.Services
.AddIdentityApiEndpoints(opt =>
{
opt.User = new UserOptions
{
RequireUniqueEmail = true
};
opt.Password = new PasswordOptions
{
RequiredLength = 8,
RequiredUniqueChars = 0,
RequireNonAlphanumeric = true,
RequireLowercase = true,
RequireUppercase = true,
RequireDigit = true
};
opt.Lockout = new LockoutOptions
{
AllowedForNewUsers = false,
MaxFailedAccessAttempts = 3,
DefaultLockoutTimeSpan = new TimeSpan(8, 0, 0) // 8 hours lockout
};
opt.SignIn = new SignInOptions
{
RequireConfirmedEmail = true,
//RequireConfirmedAccount = true
};
// opt.Tokens = new TokenOptions() { };
})
.AddEntityFrameworkStores();
builder.Services.AddAuthorization();
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(opt =>
{
opt.SwaggerDoc("v1", new OpenApiInfo { Title = "IspH2H.WebAPI", Version = "v1" });
opt.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
In = ParameterLocation.Header,
Description = "Please enter token",
Name = "Authorization",
Type = SecuritySchemeType.Http,
BearerFormat = "JWT",
Scheme = "bearer"
});
opt.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type=ReferenceType.SecurityScheme,
Id="Bearer"
}
},
new string[]{}
}
});
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
// Adds the endpoint
// /migrate
// for migrations
app.UseMigrationsEndPoint(new MigrationsEndPointOptions
{
Path = "/migrate"
});
app.MapGet("/contexts", (HttpContext context) =>
{
var registeredContextNames = context.RequestServices.GetServices()
.Select(o => o.ContextType.AssemblyQualifiedName).ToList();
return registeredContextNames;
});
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapIdentityApi(); // New in.net 8, it adds all the authentication endpoints
app.MapControllers();
app.Run();
to configure asp.net identity and add endpoints
At the end I can see the endpoint in swagger
but I can’t find documentation for calls.