Example #1
0
 private static Users InitUserData(Users user, MyEnums.UserTypes userType, Guid?parentUserId = null)
 {
     user.RegisterDate = DateTime.Now;
     user.Type         = userType;
     user.Status       = MyEnums.UserStatus.Active;
     user.Username     = user.Username.ToLower();
     user.ParentUserId = parentUserId;
     return(user);
 }
Example #2
0
        public static string BuildToken(Models.Users user, IConfiguration config, MyEnums.UserTypes type)
        {
            var claims = new[] {
                new Claim("Name", user.Name),
                new Claim("Status", user.Status.ToString()),
                new Claim("ID", user.Id.ToString()),
                new Claim(ClaimTypes.Role, type == MyEnums.UserTypes.Admin ? "ADMIN" : "DEVICE")
            };

            var key   = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(config["Jwt:Key"]));
            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

            var token = new JwtSecurityToken(config["Jwt:Issuer"],
                                             config["Jwt:Issuer"],
                                             claims,
                                             expires: DateTime.Now.AddDays(1),
                                             signingCredentials: creds);

            return(new JwtSecurityTokenHandler().WriteToken(token));
        }