예제 #1
0
        static async Task Main(string[] args)
        {
            var app = WebApplication.Create(args);

            var todos = new TodoApi();

            todos.MapRoutes(app);

            await app.RunAsync();
        }
예제 #2
0
        static async Task Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            builder.Services.AddDbContext <TodoDbContext>(options => options.UseInMemoryDatabase("Todos"));

            var app = builder.Build();

            TodoApi.MapRoutes(app);

            await app.RunAsync();
        }
예제 #3
0
파일: Program.cs 프로젝트: stevenknox/Todos
        static async Task Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            var jwtSettings = JwtSettings.FromConfiguration(builder.Configuration);

            builder.Services.AddIdentityCore <TodoUser>()
            .AddEntityFrameworkStores <TodoDbContext>();

            builder.Services.AddDbContext <TodoDbContext>(options => options.UseInMemoryDatabase("Todos"));

            builder.Services.AddAuthorization(options =>
            {
                options.AddPolicy("admin", policy => policy.RequireClaim("can_delete", "true"));
                options.AddPolicy("user", policy => policy.RequireClaim("can_view", "true"));
            });

            builder.Services
            .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options => options.TokenValidationParameters = jwtSettings.TokenValidationParameters);

            var app = builder.Build();

            using (var scope = app.Services.CreateScope())
            {
                var userManager = scope.ServiceProvider.GetService <UserManager <TodoUser> >();
                await userManager.CreateAsync(new TodoUser { UserName = "******", IsAdmin = true }, "Pass123456!");
            }

            app.UseAuthentication();
            app.UseAuthorization();

            var auth = new AuthApi(jwtSettings);

            auth.MapRoutes(app);

            var todo = new TodoApi();

            todo.MapRoutes(app);

            await app.RunAsync();
        }