SHARE
TWEET

Untitled

a guest Jun 16th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3.  
  4.             /*...*/
  5.  
  6.             var key = Encoding.ASCII.GetBytes("9ST5hQe5dUNfAJOQZAtt19uiDhNtKKUt");
  7.             var signingKey = new SymmetricSecurityKey(key);
  8.  
  9.             // Authenticate a request
  10.             services.AddAuthentication(x =>
  11.             {
  12.                 x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
  13.                 x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
  14.             })
  15.             .AddJwtBearer(x =>
  16.             {
  17.                 x.RequireHttpsMetadata = false;
  18.                 x.SaveToken = true;
  19.                 x.TokenValidationParameters = new TokenValidationParameters
  20.                 {
  21.                     ValidateIssuerSigningKey = true,
  22.                     IssuerSigningKey = signingKey,
  23.                     ValidateAudience = false,
  24.                     ValidateIssuer = false
  25.                 };
  26.             });
  27.             // Custom policy to check if a certain claim has a certain value
  28.             services.AddAuthorization(options =>
  29.             {
  30.                 options.AddPolicy(
  31.                     "IsAgentPolicy",
  32.                     policy => policy.RequireClaim("aut", "ROLE_AGENT")
  33.                 );
  34.             });
  35.  
  36.             /*...*/
  37. }
  38.  
  39. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  40.         {
  41.             if (env.IsDevelopment())
  42.             {
  43.                 app.UseDeveloperExceptionPage();
  44.             }
  45.  
  46.             app.UseAuthentication();
  47.  
  48.             app.UseMvc();
  49.         }
  50.      
  51. [Route("api/[controller]")]
  52.     public class ValuesController : ControllerBase
  53.     {
  54.         // GET: api/<controller>
  55.         [HttpGet]
  56.         public IEnumerable<string> Get()
  57.         {
  58.             return new string[] { "value1", "value2" };
  59.         }
  60.  
  61.         // GET api/<controller>/5
  62.         [HttpGet("{id}")]
  63.         [Authorize("IsAgentPolicy")]
  64.         public string Get(int id)
  65.         {
  66.             return "value";
  67.         }
  68.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top