SHARE
TWEET

Untitled

a guest Jan 21st, 2020 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.IdentityModel.Tokens.Jwt;
  3. using System.IO;
  4. using System.Security.Cryptography.X509Certificates;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Microsoft.AspNetCore.Authentication.JwtBearer;
  8. using Microsoft.AspNetCore.Builder;
  9. using Microsoft.AspNetCore.Hosting;
  10. using Microsoft.AspNetCore.Mvc;
  11. using Microsoft.Extensions.Configuration;
  12. using Microsoft.Extensions.DependencyInjection;
  13. using Microsoft.IdentityModel.Protocols;
  14. using Microsoft.IdentityModel.Protocols.OpenIdConnect;
  15. using Microsoft.IdentityModel.Tokens;
  16.  
  17. namespace OidcApi
  18. {
  19.     public class NullConfigurationManager : IConfigurationManager<OpenIdConnectConfiguration>
  20.     {
  21.         public Task<OpenIdConnectConfiguration> GetConfigurationAsync(CancellationToken cancel)
  22.         {
  23.             return Task.FromResult((OpenIdConnectConfiguration) null);
  24.         }
  25.  
  26.         public void RequestRefresh()
  27.         {
  28.         }
  29.     }
  30.  
  31.     public class Startup
  32.     {
  33.         public Startup(IConfiguration configuration)
  34.         {
  35.             Configuration = configuration;
  36.         }
  37.  
  38.         public IConfiguration Configuration { get; }
  39.  
  40.         public void ConfigureServices(IServiceCollection services)
  41.         {
  42.             var cert = new X509Certificate2(File.ReadAllBytes("pkcs12public_dev_usb_123.pfx"), "123");
  43.             var key = new X509SecurityKey(cert);
  44.             var jwk = JsonWebKeyConverter.ConvertFromX509SecurityKey(key);
  45.  
  46.             services.AddMvc()
  47.                 .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  48.  
  49.             services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
  50.                 .AddJwtBearer(options =>
  51.                 {
  52.                     options.SecurityTokenValidators.Clear();
  53.                     options.SecurityTokenValidators.Add(
  54.                         new JwtSecurityTokenHandler
  55.                         {
  56.                             MapInboundClaims = false
  57.                         });
  58.                     options.ConfigurationManager = new NullConfigurationManager();
  59.                     options.TokenValidationParameters = new TokenValidationParameters
  60.                     {
  61.                         ValidIssuer = "https://oidc-auth-dev-usb.dodois.ru",
  62.                         ValidAudience = "delivery_cashier",
  63.                         IssuerSigningKeys = new SecurityKey[] {jwk},
  64.                         ValidateActor = false,
  65.                         ValidateAudience = true,
  66.                         ValidateIssuer = true,
  67.                         ValidateIssuerSigningKey = true,
  68.                         ValidateLifetime = true,
  69.                         ValidateTokenReplay = true,
  70.                         RequireExpirationTime = true,
  71.                         RequireSignedTokens = true,
  72.                         ClockSkew = TimeSpan.FromSeconds(5),
  73.                         NameClaimType = Microsoft.IdentityModel.JsonWebTokens.JwtRegisteredClaimNames.Sub,
  74.                         RoleClaimType = "role"
  75.                     };
  76.                 });
  77.         }
  78.  
  79.         public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  80.         {
  81.             if (env.IsDevelopment())
  82.             {
  83.                 app.UseDeveloperExceptionPage();
  84.             }
  85.             else
  86.             {
  87.                 app.UseHsts();
  88.             }
  89.  
  90.             app.UseAuthentication();
  91.             app.UseHttpsRedirection();
  92.             app.UseMvc();
  93.         }
  94.     }
  95. }
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