1

I'm trying to use all the new features in ASP.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<SmtpEmailSenderConfiguration>(builder.Configuration.GetSection(SmtpEmailSenderConfiguration.SmtpEmailSender));
builder.Services.AddSingleton<IEmailSender<IspH2HIdentityUser>, SmtpEmailSender>();

builder.Services.AddDbContext<IspH2HContext>(opt =>
    opt.UseSqlServer(builder.Configuration.GetConnectionString("Default")));

builder.Services
    .AddIdentityApiEndpoints<IspH2HIdentityUser>(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<IspH2HContext>();

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<DbContextOptions>()
            .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 Core identity and add endpoints.

At the end I can see the endpoint in Swagger:

Swagger_output

but I can't find documentation for calls.

2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.