Как реализовать двухфакторную аутентификацию в приложении SPA с идентификатором asp net core 8.0 в качестве бэкэндаC#

Место общения программистов C#
Anonymous
Как реализовать двухфакторную аутентификацию в приложении SPA с идентификатором asp net core 8.0 в качестве бэкэнда

Сообщение Anonymous »

Я пытаюсь использовать все новые возможности .net core 8.
Я использую этот код

Код: Выделить всё

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();
для настройки идентификации asp.net и добавления конечных точек
В конце я вижу конечную точку в Swagger
Изображение

но я не могу найти документацию для вызовов.

Подробнее здесь: https://stackoverflow.com/questions/786 ... p-net-core

Вернуться в «C#»