public string GenerateTokenForUser(TBUsers users)
        {
            var claims = new[] {
                new Claim("id", users.UserId.ToString()),
                new Claim("mobileNumber", users.MobileNumber),
                new Claim("name", users.Name),
                new Claim("role", "USER")
            };

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

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

            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject            = new ClaimsIdentity(claims),
                Expires            = DateTime.Now.AddHours(1),
                SigningCredentials = credentialsKey
            };

            var tokenHandler = new JwtSecurityTokenHandler();
            var token        = tokenHandler.CreateToken(tokenDescriptor);

            return(tokenHandler.WriteToken(token));
        }
Example #2
0
        public async Task <IActionResult> UserSignUp([FromBody] TBUsers user)
        {
            if (await _authrepo.IsUserExists(user.Email))
            {
                return(Forbid());                                          // User already exists
            }
            var userToCreate = new TBUsers
            {
                MobileNumber = user.MobileNumber,
                Name         = user.Name,
                Email        = user.Email,
                Password     = _pwd.Encrypt(user.Password),
                Role         = user.Role,
                Active       = user.Active
            };

            var createUser = await _authrepo.CreateNewUser(userToCreate);

            var generateToken = _authrepo.GenerateTokenForUser(createUser);

            return(Ok(new
            {
                token = generateToken
            }));
        }
        public async Task <TBUsers> CreateNewUser(TBUsers users)
        {
            await _database.tbusers.AddAsync(users);

            await _database.SaveChangesAsync();

            return(users);
        }