public JwtSigningOptions(IJwtOptions jwtOptions) { byte[] key = Encoding.UTF8.GetBytes(jwtOptions.SecretKey); SymmetricSecurityKey securityKey = new SymmetricSecurityKey(key); SigningCredentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature); }
/// <summary> /// Generates tokens for users /// </summary> /// <param name="logger">A logger</param> /// <param name="context">The DB</param> /// <param name="options">JWT config options</param> public TokenGenerator(ILogger <TokenGenerator> logger, Db context, IJwtOptions options) { this._tokenHandler = new JwtSecurityTokenHandler(); this._logger = logger; this._options = options; this._context = context; }
public GenerateWebTokenHandler(IJwtSigningOptions jwtSigningOptions, IJwtOptions jwtOptions, IRefreshTokenRepository refreshTokenRepository) { _jwtSigningOptions = jwtSigningOptions; _jwtOptions = jwtOptions; _jwtSecurityTokenHandler = new JwtSecurityTokenHandler(); _refreshTokenRepository = refreshTokenRepository; }
public JwtService( IJwtOptions jwtOptions, UserManager <LocalistUser?> userManager) { this.jwtOptions = jwtOptions; this.userManager = userManager; }
public JwtService(IJwtOptions jwtOptions) { _jwtOptions = jwtOptions; var issuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtOptions.SecretKey)); _signingCredentials = new SigningCredentials(issuerSigningKey, SecurityAlgorithms.HmacSha256); }
public AuthService(IJwtHelper jwtHelper, IDnsWatcherDbContext context, IRandomGenerator randomGenerator, IDateTime dateTime, IJwtOptions jwtOptions) { _jwtHelper = jwtHelper; _context = context; _randomGenerator = randomGenerator; _dateTime = dateTime; _jwtOptions = jwtOptions; }
public static void ConfigureJwtAuth(this IServiceCollection services, IJwtOptions jwtOptions) { services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => options.TokenValidationParameters = new TokenValidationParameters() { ValidateIssuer = true, ValidIssuer = jwtOptions.Issuer, ValidateAudience = true, ValidAudience = jwtOptions.Audience, IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtOptions.SecretKey)), ValidateIssuerSigningKey = true }); }
private static JwtSecurityToken GenerateToken(IJwtOptions jwtOptions, IEnumerable <Claim> claims) { if (jwtOptions == null) { throw new ArgumentNullException(nameof(jwtOptions)); } var now = DateTime.UtcNow; return(new JwtSecurityToken( jwtOptions.Issuer, jwtOptions.Audience, notBefore: now, claims: claims, expires: now.Add(TimeSpan.FromMinutes(jwtOptions.Lifetime)), signingCredentials: new SigningCredentials(jwtOptions.GetSymmetricSecurityKey(), SecurityAlgorithms.HmacSha256))); }
private static void ThrowIfInvalidOptions(IJwtOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } if (options.ValidFor <= TimeSpan.Zero) { throw new ArgumentException("Must be a non-zero TimeSpan.", nameof(JwtIssuerOptions.ValidFor)); } if (options.SigningCredentials == null) { throw new ArgumentNullException(nameof(JwtIssuerOptions.SigningCredentials)); } if (options.JtiGenerator == null) { throw new ArgumentNullException(nameof(JwtIssuerOptions.JtiGenerator)); } }
private void ConfigureAppSettings(IServiceCollection services) { void ConfigureSection <Interface, Implementation>(string sectionName) where Implementation : Interface, new() where Interface : class { Implementation configSection = new Implementation(); Configuration.GetSection(sectionName).Bind(configSection); services.AddSingleton <Interface>(configSection); } ConfigureSection <IJwtOptions, JwtOptions>(nameof(JwtOptions)); ConfigureSection <IConnectionStrings, ConnectionStrings>(nameof(ConnectionStrings)); ConfigureSection <IMongodbDatabaseSettings, MongodbDatabaseSettings>(nameof(MongodbDatabaseSettings)); services.AddSingleton <IAppSettings>(factory => { IJwtOptions jwtOptionsSection = factory.GetService <IJwtOptions>(); IConnectionStrings connectionStringsSection = factory.GetService <IConnectionStrings>(); IMongodbDatabaseSettings mongodbDatabaseSettings = factory.GetService <IMongodbDatabaseSettings>(); return(new AppSettings(jwtOptionsSection, connectionStringsSection, mongodbDatabaseSettings)); }); }
public JwtHelper(IJwtOptions jwtOptions, IDateTime dateTime) { _jwtOptions = jwtOptions; _dateTime = dateTime; }
public JwtAccessTokenGenerator(IJwtOptions jwtOptions) { _jwtOptions = jwtOptions; }
public AuthenticationService(IJwtOptions jwtOptions) { _jwtOptions = jwtOptions; }
public AppSettings(IJwtOptions jwtOptions, IConnectionStrings connectionStrings, IMongodbDatabaseSettings mongodbDatabaseSettings) { JwtOptions = jwtOptions; ConnectionStrings = connectionStrings; MongodbDatabaseSettings = mongodbDatabaseSettings; }