public AppUserDm InsertUser(AppUserDm user) { PiContext.ApplicationUsers.Add(user); PiContext.SaveChanges(); return(user); }
private Task <ClaimsIdentity> CreateClaimsIdentities(AppUserDm user, IEnumerable <string> roles) { ClaimsIdentity claimsIdentity = new ClaimsIdentity(); claimsIdentity.AddClaim(new Claim(ClaimTypes.Email, user.Email)); claimsIdentity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id.ToString())); claimsIdentity.AddClaim(new Claim(ClaimTypes.Name, user.Username)); claimsIdentity.AddClaim(new Claim(ClaimTypes.GivenName, user.DisplayName)); claimsIdentity.AddClaim(new Claim(ClaimTypes.UserData, JsonConvert.SerializeObject(new { DisplayName = user.DisplayName }))); foreach (var role in roles) { claimsIdentity.AddClaim(new Claim(ClaimTypes.Role, role)); } return(Task.FromResult(claimsIdentity)); }
public void CreateUser(string username, string password) { var user = new AppUserDm() { ActiveFrom = DateTime.Now, ActiveTo = DateTime.Now.AddYears(100), CreatedDate = DateTime.Now, DisplayName = username, Username = username }; var hashedPassword = passwordHashing.HashPassword(password); user.Password = hashedPassword.Password; user.Salt = hashedPassword.Salt; userRepository.InsertUser(user); }
public async Task <string> CreateJwtAsync( AppUserDm user, IEnumerable <string> roles, int daysValid) { var tokenHandler = new JwtSecurityTokenHandler(); var claims = await CreateClaimsIdentities(user, roles); var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(apiConfig.PrivateKey)); var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); var token = tokenHandler.CreateJwtSecurityToken( issuer: apiConfig.Issuer, audience: apiConfig.Issuer, subject: claims, notBefore: DateTime.UtcNow, expires: DateTime.UtcNow.AddDays(daysValid), signingCredentials: creds); return(tokenHandler.WriteToken(token)); }