コード例 #1
0
        public static void RegisterJWT(this IServiceCollection service, JWTConfigurationModel JwtSection)
        {
            service.AddScoped <ICurrentUser, CurrentUser>();
            service.AddScoped <IJWTService>(provider => new JWTService(JwtSection));


            var key = Encoding.ASCII.GetBytes(JwtSection.Secret);

            service.AddAuthentication(option =>
            {
                option.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                option.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(option =>
            {
                option.RequireHttpsMetadata      = false;
                option.SaveToken                 = true;
                option.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(key),
                    ValidateIssuer           = false,
                    ValidateAudience         = false,
                };
            });
        }
コード例 #2
0
 public JWTService(JWTConfigurationModel configuration)
 {
     this.configuration = configuration;
 }