public static string GenerateAuthToken(int userId)
        {
            string jwtKey = ConfigContex.GetJwtKey();

            var SymmetricSecurityLey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtKey));
            // Signing Credentials
            var signingCredentials = new SigningCredentials(SymmetricSecurityLey, SecurityAlgorithms.HmacSha256);

            // Claims
            var claims = new[]
            {
                new Claim(JwtRegisteredClaimNames.Sub, userId.ToString()),
            };

            // Create Token
            var token = new JwtSecurityToken(
                issuer: ConfigContex.GetJwtIssuer(),
                audience: ConfigContex.GetJwtAudience(),
                claims,
                expires: DateTime.Now.AddDays(7),
                signingCredentials: signingCredentials
                );
            // Encode Token
            var encodeToken = new JwtSecurityTokenHandler().WriteToken(token);

            // Return Token
            return(encodeToken);
        }
 // Returns validation parameter settings
 private static TokenValidationParameters GetValidationParameters()
 {
     return(new TokenValidationParameters()
     {
         ValidateLifetime = true,
         ValidIssuer = ConfigContex.GetJwtIssuer(),
         ValidAudience = ConfigContex.GetJwtIssuer(),
         IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(ConfigContex.GetJwtKey()))
     });
 }
Пример #3
0
        public static string HashPassword(string unHashedPassword)
        {
            // Salt
            byte[] salt = Encoding.ASCII.GetBytes(ConfigContex.GetSalt());

            // Hash password
            var pbkdf2 = new Rfc2898DeriveBytes(unHashedPassword, salt, 10000);

            byte[] hash = pbkdf2.GetBytes(20);

            // Combine salt and Password
            byte[] hashBytes = new byte[36];
            Array.Copy(salt, 0, hashBytes, 0, 16);
            Array.Copy(hash, 0, hashBytes, 16, 20);

            // Stringyfy password and return it
            string hasedPassword = Convert.ToBase64String(hashBytes);

            return(hasedPassword);
        }