Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
63 lines (54 sloc)
2.14 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Authentication.JwtBearer; | |
using Microsoft.AspNetCore.Builder; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Hosting; | |
using System.Security.Claims; | |
namespace Resource.Api | |
{ | |
public class Startup | |
{ | |
public Startup(IConfiguration configuration) | |
{ | |
Configuration = configuration; | |
} | |
public IConfiguration Configuration { get; } | |
// This method gets called by the runtime. Use this method to add services to the container. | |
public static void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddAuthentication(options => | |
{ | |
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; | |
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; | |
}).AddJwtBearer(o => | |
{ | |
o.Authority = "http://localhost:5000"; | |
o.Audience = "resourceapi"; | |
o.RequireHttpsMetadata = false; | |
}); | |
services.AddAuthorization(options => | |
{ | |
options.AddPolicy("ApiReader", policy => policy.RequireClaim("scope", "api.read")); | |
options.AddPolicy("Consumer", policy => policy.RequireClaim(ClaimTypes.Role, "consumer")); | |
}); | |
services.AddMvc(options => | |
{ | |
options.EnableEndpointRouting = false; | |
}) | |
.SetCompatibilityVersion(CompatibilityVersion.Latest); | |
} | |
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | |
public static void Configure(IApplicationBuilder app, IWebHostEnvironment env) | |
{ | |
if (env.IsDevelopment()) | |
{ | |
app.UseDeveloperExceptionPage(); | |
} | |
app.UseCors(options => options.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin()); | |
app.UseAuthentication(); | |
app.UseMvc(); | |
} | |
} | |
} |