コード例 #1
0
        private string GenerateJwtToken(Mem_Master user, string uid)
        {
            var claims = new List <Claim>
            {
                new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
                new Claim(ClaimTypes.Name, user.Name),
                new Claim(ClaimTypes.Sid, uid)
            };

            // var roles = await _userManager.GetRolesAsync(user);

            // foreach (var role in roles)
            // {
            //     claims.Add(new Claim(ClaimTypes.Role, role));
            // }

            var key = new SymmetricSecurityKey(Encoding.UTF8
                                               .GetBytes(_config.GetSection("AppSettings:Token").Value));

            var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature);

            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject            = new ClaimsIdentity(claims),
                SigningCredentials = credentials,
                Expires            = DateTime.UtcNow.AddMonths(6)
            };

            var tokenHandler = new JwtSecurityTokenHandler();

            var token = tokenHandler.CreateToken(tokenDescriptor);

            return(tokenHandler.WriteToken(token));
        }
コード例 #2
0
        public async Task <bool> Update(Mem_Master User, string password, string newPassword = null)
        {
            if (!VerifyPasswordHash(password, User.PasswordHash, User.PasswordSalt))
            {
                return(false);
            }

            if (newPassword != null)
            {
                CreatePasswordHash(newPassword, out var passwordHash, out var passwordSalt);
                User.PasswordHash = passwordHash;
                User.PasswordSalt = passwordSalt;
            }

            _db.Update(User);
            try
            {
                await _db.SaveChangesAsync();

                return(true);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
コード例 #3
0
        public async Task <bool> ResetPassword(Mem_Master user, string password)
        {
            if (password != null)
            {
                CreatePasswordHash(password, out var passwordHash, out var passwordSalt);
                user.PasswordHash = passwordHash;
                user.PasswordSalt = passwordSalt;
            }
            _db.Update(user);
            try
            {
                await _db.SaveChangesAsync();

                return(true);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
コード例 #4
0
 public IActionResult Index()
 {
     if (!_db.Mem_Masters.Any())
     {
         _auth.CreatePasswordHash("123456", out var passwordHash, out var passwordSalt);
         var newMem = new Mem_Master
         {
             AdNo         = "111",
             Name         = "Jane Doe",
             DateOfBirth  = DateTime.Now,
             IsGroupAdmin = false,
             IsVarified   = false,
             PasswordSalt = passwordSalt,
             PasswordHash = passwordHash,
             CountryCode  = "91",
             Phone        = "9969969961",
             CDate        = DateTime.Now,
             Email        = "janedoe"
         };
         _db.Mem_Masters.Add(newMem);
         _db.SaveChanges();
     }
     return(View());
 }
コード例 #5
0
        public async Task <bool> ResetAsync(Mem_Master user, string password)
        {
            var key = Generate(8, 2);

            return(false);
        }