示例#1
0
        public JwtSigningOptions(IJwtOptions jwtOptions)
        {
            byte[] key = Encoding.UTF8.GetBytes(jwtOptions.SecretKey);
            SymmetricSecurityKey securityKey = new SymmetricSecurityKey(key);

            SigningCredentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature);
        }
示例#2
0
 /// <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;
 }
示例#3
0
 public GenerateWebTokenHandler(IJwtSigningOptions jwtSigningOptions, IJwtOptions jwtOptions, IRefreshTokenRepository refreshTokenRepository)
 {
     _jwtSigningOptions       = jwtSigningOptions;
     _jwtOptions              = jwtOptions;
     _jwtSecurityTokenHandler = new JwtSecurityTokenHandler();
     _refreshTokenRepository  = refreshTokenRepository;
 }
示例#4
0
 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);
        }
示例#6
0
 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
     });
 }
示例#8
0
        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)));
        }
示例#9
0
        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));
            }
        }
示例#10
0
        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));
            });
        }
示例#11
0
 public JwtHelper(IJwtOptions jwtOptions, IDateTime dateTime)
 {
     _jwtOptions = jwtOptions;
     _dateTime   = dateTime;
 }
示例#12
0
 public JwtAccessTokenGenerator(IJwtOptions jwtOptions)
 {
     _jwtOptions = jwtOptions;
 }
示例#13
0
 public AuthenticationService(IJwtOptions jwtOptions)
 {
     _jwtOptions = jwtOptions;
 }
 public AppSettings(IJwtOptions jwtOptions, IConnectionStrings connectionStrings, IMongodbDatabaseSettings mongodbDatabaseSettings)
 {
     JwtOptions              = jwtOptions;
     ConnectionStrings       = connectionStrings;
     MongodbDatabaseSettings = mongodbDatabaseSettings;
 }