public static void BuildApp(this IApplicationBuilder app) { app.UseRouter(r => { var contactRepo = new InMemoryContactRepository(); r.MapGet("contacts", async(request, response, routeData) => { var contacts = await contactRepo.GetAll(); await response.WriteJson(contacts); }); r.MapGet("contacts/{id:int}", async(request, response, routeData) => { var contact = await contactRepo.Get(Convert.ToInt32(routeData.Values["id"])); if (contact == null) { response.StatusCode = 404; return; } await response.WriteJson(contact); }); r.MapPost("contacts", async(request, response, routeData) => { var newContact = await request.HttpContext.ReadFromJson <Contact>(); if (newContact == null) { return; } await contactRepo.Add(newContact); response.StatusCode = 201; await response.WriteJson(newContact); }); r.MapPut("contacts/{id:int}", async(request, response, routeData) => { var updatedContact = await request.HttpContext.ReadFromJson <Contact>(); if (updatedContact == null) { return; } updatedContact.ContactId = Convert.ToInt32(routeData.Values["id"]); await contactRepo.Update(updatedContact); response.StatusCode = 204; }); r.MapDelete("contacts/{id:int}", async(request, response, routeData) => { await contactRepo.Delete(Convert.ToInt32(routeData.Values["id"])); response.StatusCode = 204; }); }); }
public static void Main(string[] args) => new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .ConfigureAppConfiguration((hostingContext, config) => { config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).AddEnvironmentVariables(); }) .ConfigureLogging((hostingContext, l) => { l.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); l.AddConsole(); }) .UseIISIntegration() .ConfigureServices(s => { // set up embedded identity server s.AddIdentityServer(). AddTestClients(). AddTestResources(). AddDeveloperSigningCredential(); s.AddRouting() .AddAuthorization(options => { // set up authorization policy for the API options.AddPolicy("API", policy => { policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme); policy.RequireAuthenticatedUser().RequireClaim("scope", "read"); }); }) .AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme) .AddIdentityServerAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme, o => { o.Authority = "http://localhost:5000/openid"; o.RequireHttpsMetadata = false; }); }) .Configure(app => { app.Map("/openid", id => { // use embedded identity server to issue tokens id.UseIdentityServer(); }) .UseAuthentication() // consume the JWT tokens in the API .Use(async(c, next) => // authorize the whole API against the API policy { var allowed = await c.RequestServices.GetRequiredService <IAuthorizationService>().AuthorizeAsync(c.User, null, "API"); if (allowed.Succeeded) { await next(); } else { c.Response.StatusCode = 401; } }) .UseRouter(r => // define all API endpoints { var contactRepo = new InMemoryContactRepository(); r.MapGet("contacts", async(request, response, routeData) => { var contacts = await contactRepo.GetAll(); response.WriteJson(contacts); }); r.MapGet("contacts/{id:int}", async(request, response, routeData) => { var contact = await contactRepo.Get(Convert.ToInt32(routeData.Values["id"])); if (contact == null) { response.StatusCode = 404; return; } response.WriteJson(contact); }); }); }) .Build().Run();
public static async Task Main(string[] args) => await WebHost.CreateDefaultBuilder(args) .ConfigureServices(s => { s.AddEmbeddedIdentityServer(); s.AddRouting(); }) .Configure(app => { app.Map("/identity", id => { // use embedded identity server to issue tokens id.UseIdentityServer(); }) .UseAuthentication() // consume the JWT tokens in the API .Use(async(c, next) => // authorize the whole API against the API policy { var allowed = await c.RequestServices.GetRequiredService <IAuthorizationService>().AuthorizeAsync(c.User, "API"); if (!allowed.Succeeded) { c.Response.StatusCode = 401; return; } await next(); }) .UseRouter(r => // define all API endpoints { var contactRepo = new InMemoryContactRepository(); r.MapGet("contacts", async(request, response, routeData) => { var contacts = await contactRepo.GetAll(); response.WriteJson(contacts); }); r.MapGet("contacts/{id:int}", async(request, response, routeData) => { var contact = await contactRepo.Get(Convert.ToInt32(routeData.Values["id"])); if (contact == null) { response.StatusCode = 404; return; } response.WriteJson(contact); }); r.MapPost("contacts", async(request, response, routeData) => { var newContact = request.HttpContext.ReadFromJson <Contact>(); if (newContact == null) { return; } await contactRepo.Add(newContact); response.StatusCode = 201; response.WriteJson(newContact); }); r.MapPut("contacts/{id:int}", async(request, response, routeData) => { var updatedContact = request.HttpContext.ReadFromJson <Contact>(); if (updatedContact == null) { return; } updatedContact.ContactId = Convert.ToInt32(routeData.Values["id"]); await contactRepo.Update(updatedContact); response.StatusCode = 204; }); r.MapDelete("contacts/{id:int}", async(request, response, routeData) => { await contactRepo.Delete(Convert.ToInt32(routeData.Values["id"])); response.StatusCode = 204; }); }); }) .Build().RunAsync();
public static void Main(string[] args) { var config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables().Build(); var host = new WebHostBuilder() .UseKestrel() .UseConfiguration(config) .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .ConfigureLogging(l => l.AddConsole(config.GetSection("Logging"))) .ConfigureServices(s => { // set up embedded identity server s.AddIdentityServer(). AddTestClients(). AddTestResources(). AddTemporarySigningCredential(); s.AddRouting(); // set up authorization policy for the API s.AddAuthorization(options => { options.AddPolicy("API", policy => { policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme); policy.RequireAuthenticatedUser().RequireClaim("scope", "read"); }); }); }) .Configure(app => { // use embedded identity server to issue tokens app.UseIdentityServer(); // consume the JWT tokens in the API app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions { Authority = "http://localhost:34917", RequireHttpsMetadata = false, }); // authorize the whole API against the API policy app.Use(async(c, next) => { var authz = c.RequestServices.GetRequiredService <IAuthorizationService>(); var allowed = await authz.AuthorizeAsync(c.User, null, "API"); if (allowed) { await next(); } else { c.Response.StatusCode = 401; } }); // define all API endpoints app.UseRouter(r => { var contactRepo = new InMemoryContactRepository(); r.MapGet("contacts", async(request, response, routeData) => { var contacts = await contactRepo.GetAll(); await response.WriteJson(contacts); }); r.MapGet("contacts/{id:int}", async(request, response, routeData) => { var contact = await contactRepo.Get(Convert.ToInt32(routeData.Values["id"])); if (contact == null) { response.StatusCode = 404; return; } await response.WriteJson(contact); }); }); }) .Build(); host.Run(); }
public static async Task Main(string[] args) => await WebHost.CreateDefaultBuilder(args) .ConfigureServices(s => { // set up embedded identity server s.AddIdentityServer(). AddTestClients(). AddTestResources(). AddDeveloperSigningCredential(); s.AddRouting() .AddAuthorization(options => { // set up authorization policy for the API options.AddPolicy("API", policy => { policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme); policy.RequireAuthenticatedUser().RequireClaim("scope", "read"); }); }) .AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme) .AddIdentityServerAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme, o => { o.Authority = "https://localhost:5001/identity"; }); }) .Configure(app => { app.Map("/identity", id => { // use embedded identity server to issue tokens id.UseIdentityServer(); }) .UseAuthentication() // consume the JWT tokens in the API .Use(async(c, next) => // authorize the whole API against the API policy { var allowed = await c.RequestServices.GetRequiredService <IAuthorizationService>().AuthorizeAsync(c.User, null, "API"); if (allowed.Succeeded) { await next(); } else { c.Response.StatusCode = 401; } }) .UseRouter(r => // define all API endpoints { var contactRepo = new InMemoryContactRepository(); r.MapGet("contacts", async(request, response, routeData) => { var contacts = await contactRepo.GetAll(); response.WriteJson(contacts); }); r.MapGet("contacts/{id:int}", async(request, response, routeData) => { var contact = await contactRepo.Get(Convert.ToInt32(routeData.Values["id"])); if (contact == null) { response.StatusCode = 404; return; } response.WriteJson(contact); }); r.MapPost("contacts", async(request, response, routeData) => { var newContact = request.HttpContext.ReadFromJson <Contact>(); if (newContact == null) { return; } await contactRepo.Add(newContact); response.StatusCode = 201; response.WriteJson(newContact); }); r.MapPut("contacts/{id:int}", async(request, response, routeData) => { var updatedContact = request.HttpContext.ReadFromJson <Contact>(); if (updatedContact == null) { return; } updatedContact.ContactId = Convert.ToInt32(routeData.Values["id"]); await contactRepo.Update(updatedContact); response.StatusCode = 204; }); r.MapDelete("contacts/{id:int}", async(request, response, routeData) => { await contactRepo.Delete(Convert.ToInt32(routeData.Values["id"])); response.StatusCode = 204; }); }); }) .Build().RunAsync();