示例#1
0
 private static RSAParameters FromJson(string path, string fileName)
 {
     try {
         using (var stream = new StreamReader(new FileStream(Path.Combine(path, fileName), FileMode.Open))) {
             var content = stream.ReadToEnd();
             return(JsonConvert.DeserializeObject <RSAParametersWithPrivate>(content).ToRSAParameters());
         }
     }
     catch {
         RsaHelper.GenerateRsaKeys(path);
         using (var stream = new StreamReader(new FileStream(Path.Combine(path, fileName), FileMode.Open))) {
             var content = stream.ReadToEnd();
             return(JsonConvert.DeserializeObject <RSAParametersWithPrivate>(content).ToRSAParameters());
         }
     }
 }
示例#2
0
        public static void AddJwtAuthentication(this IServiceCollection serviceCollection,
                                                string rsaKeyPath,
                                                string fileName,
                                                string audience,
                                                string issuer)
        {
            var rsaSecurityKey = RsaHelper.GetRsaSecurityKey(rsaKeyPath, fileName);

            serviceCollection.AddSingleton(new TokenAuthOption {
                Audience           = audience,
                Issuer             = issuer,
                SigningCredentials = new SigningCredentials(rsaSecurityKey, SecurityAlgorithms.RsaSha256Signature),
                Key = rsaSecurityKey
            });
            serviceCollection.AddAuthorization(auth => {
                auth.AddPolicy("Bearer", new AuthorizationPolicyBuilder()
                               .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
                               .RequireAuthenticatedUser().Build());
            });
        }
示例#3
0
        public static void UseJwtAuthentication(this IApplicationBuilder applicationBuilder,
                                                string rsaKeyPath,
                                                string fileName,
                                                string audience,
                                                string issuer)
        {
            var rsaSecurityKey = RsaHelper.GetRsaSecurityKey(rsaKeyPath, fileName);

            applicationBuilder.UseJwtBearerAuthentication(new JwtBearerOptions {
                TokenValidationParameters =
                {
                    IssuerSigningKey         = rsaSecurityKey,
                    ValidAudience            = audience,
                    ValidIssuer              = issuer,
                    ValidateIssuerSigningKey = true,
                    ValidateLifetime         = true,
                    ClockSkew                = TimeSpan.Zero
                }
            });
        }