/
githubmirror
/
aspnetcore
Обзор
Документация
Войти
/
githubmirror
/
aspnetcore
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Identity/samples/IdentitySample.ApiEndpoints/Program.cs
38 строк
1 KB
Stephen Halter
Add MapIdentityApi<TUser>() (#47414)
27 апр 2023, 22:06
Не верифицирован
27 апр 2023, 22:06
d4430f0
Код
Авторство
О чём код?
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System.Security.Claims; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); var builder = WebApplication.CreateBuilder(args); builder.Services.AddAuthorization(); builder.Services.AddDbContext<ApplicationDbContext>( options => options.UseSqlite(connection)); builder.Services.AddIdentityApiEndpoints<IdentityUser>() .AddEntityFrameworkStores<ApplicationDbContext>(); var app = builder.Build(); app.MapGet("/", () => "Hello, World!"); app.MapGet("/requires-auth", (ClaimsPrincipal user) => $"Hello, {user.Identity?.Name}!").RequireAuthorization(); app.MapGroup("/identity").MapIdentityApi<IdentityUser>(); app.Run(); connection.Close(); public class ApplicationDbContext : IdentityDbContext<IdentityUser> { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { Database.EnsureCreated(); } }