/// <summary> /// 启用Jwt验证 /// </summary> /// <param name="services"></param> /// <param name="hosting"></param> public static void AddJwt(this IServiceCollection services, IWebHostEnvironment hosting) { // 从文件读取密钥 string keyDir = hosting.ContentRootPath; if (!EncryptorHelper.TryGetKeyParameters(keyDir, true, out RSAParameters keyParams)) { keyParams = EncryptorHelper.GenerateRSAKeysAndSave(keyDir); } JWTTokenOptions _tokenOptions = new JWTTokenOptions(); _tokenOptions.Key = new RsaSecurityKey(keyParams); _tokenOptions.Credentials = new SigningCredentials(_tokenOptions.Key, SecurityAlgorithms.RsaSha256Signature); _AddJwt(services, _tokenOptions); }