Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void ConfigureServices(IServiceCollection services)
- {
- /*...*/
- var key = Encoding.ASCII.GetBytes("9ST5hQe5dUNfAJOQZAtt19uiDhNtKKUt");
- var signingKey = new SymmetricSecurityKey(key);
- // Authenticate a request
- services.AddAuthentication(x =>
- {
- x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
- x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
- })
- .AddJwtBearer(x =>
- {
- x.RequireHttpsMetadata = false;
- x.SaveToken = true;
- x.TokenValidationParameters = new TokenValidationParameters
- {
- ValidateIssuerSigningKey = true,
- IssuerSigningKey = signingKey,
- ValidateAudience = false,
- ValidateIssuer = false
- };
- });
- // Custom policy to check if a certain claim has a certain value
- services.AddAuthorization(options =>
- {
- options.AddPolicy(
- "IsAgentPolicy",
- policy => policy.RequireClaim("aut", "ROLE_AGENT")
- );
- });
- /*...*/
- }
- public void Configure(IApplicationBuilder app, IHostingEnvironment env)
- {
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
- app.UseAuthentication();
- app.UseMvc();
- }
- [Route("api/[controller]")]
- public class ValuesController : ControllerBase
- {
- // GET: api/<controller>
- [HttpGet]
- public IEnumerable<string> Get()
- {
- return new string[] { "value1", "value2" };
- }
- // GET api/<controller>/5
- [HttpGet("{id}")]
- [Authorize("IsAgentPolicy")]
- public string Get(int id)
- {
- return "value";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement