Example #1
0
 private static RequestDelegate WithServices(Func <TodoApi, RequestDelegate> handler)
 {
     return(context =>
     {
         var db = context.RequestServices.GetRequiredService <TodoDbContext>();
         var api = new TodoApi(db);
         return handler(api)(context);
     });
 }
Example #2
0
        static async Task Main(string[] args)
        {
            var app = WebApplication.Create(args);

            var todos = new TodoApi();

            todos.MapRoutes(app);

            await app.RunAsync();
        }
Example #3
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();
        }
Example #4
0
        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();
        }