public async Task <AuthenticationResult> RegisterAsync(string email, string username, string password, string firstName, string lastName, string role) { // Register a new user var user = new SantapanUser() { UserName = username, Email = email, FirstName = firstName, LastName = lastName, CreatedAt = DateTime.UtcNow }; var result = await userManager.CreateAsync(user, password); if (!result.Succeeded) { return(new AuthenticationResult() { Success = false, Errors = result.Errors.Select(e => e.Description) }); } var addRoleResult = await userManager.AddToRoleAsync(user, role); if (!addRoleResult.Succeeded) { return(new AuthenticationResult() { Success = false, Errors = addRoleResult.Errors.Select(e => e.Description) }); } var updateUserResult = await userManager.UpdateAsync(user); if (!updateUserResult.Succeeded) { return(new AuthenticationResult() { Success = false, Errors = updateUserResult.Errors.Select(e => e.Description) }); } return(await GenerateAuthenticationResultForUserAsync(user)); }
private static async Task AddTestUsers( RoleManager <SantapanRole> roleManager, UserManager <SantapanUser> userManager) { var dataExists = roleManager.Roles.Any() || userManager.Users.Any(); if (dataExists) { return; } await roleManager.CreateAsync(new SantapanRole(RoleName.Admin)); await roleManager.CreateAsync(new SantapanRole(RoleName.Caterer)); await roleManager.CreateAsync(new SantapanRole(RoleName.Customer)); var adminUser = new SantapanUser { Email = "*****@*****.**", UserName = "******", FirstName = "Admin", LastName = "User", CreatedAt = DateTime.UtcNow }; await userManager.CreateAsync(adminUser, "@Abc123"); await userManager.AddToRoleAsync(adminUser, RoleName.Admin); await userManager.UpdateAsync(adminUser); var catererUser = new SantapanUser { Email = "*****@*****.**", UserName = "******", FirstName = "Caterer", LastName = "User", CreatedAt = DateTime.UtcNow }; await userManager.CreateAsync(catererUser, "@Abc123"); await userManager.AddToRoleAsync(catererUser, RoleName.Caterer); await userManager.UpdateAsync(catererUser); var catererUser2 = new SantapanUser { Email = "*****@*****.**", UserName = "******", FirstName = "Caterer2", LastName = "User", CreatedAt = DateTime.UtcNow }; await userManager.CreateAsync(catererUser2, "@Abc123"); await userManager.AddToRoleAsync(catererUser2, RoleName.Caterer); await userManager.UpdateAsync(catererUser2); var catererUser3 = new SantapanUser { Email = "*****@*****.**", UserName = "******", FirstName = "Caterer3", LastName = "User", CreatedAt = DateTime.UtcNow }; await userManager.CreateAsync(catererUser3, "@Abc123"); await userManager.AddToRoleAsync(catererUser3, RoleName.Caterer); await userManager.UpdateAsync(catererUser3); var customerUser = new SantapanUser { Email = "*****@*****.**", UserName = "******", FirstName = "Customer", LastName = "User", CreatedAt = DateTime.UtcNow }; await userManager.CreateAsync(customerUser, "@Abc123"); await userManager.AddToRoleAsync(customerUser, RoleName.Customer); await userManager.UpdateAsync(customerUser); }
private async Task <AuthenticationResult> GenerateAuthenticationResultForUserAsync(SantapanUser user) { var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.ASCII.GetBytes(jwtSettings.Secret); var claims = new List <Claim> { new Claim(JwtRegisteredClaimNames.Sub, user.Email), new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()), new Claim(JwtRegisteredClaimNames.Email, user.Email), new Claim("id", user.Id) }; var userClaims = await userManager.GetClaimsAsync(user); claims.AddRange(userClaims); var userRoles = await userManager.GetRolesAsync(user); foreach (var userRole in userRoles) { claims.Add(new Claim(ClaimTypes.Role, userRole)); var role = await roleManager.FindByNameAsync(userRole); if (role == null) { continue; } var roleClaims = await roleManager.GetClaimsAsync(role); foreach (var roleClaim in roleClaims) { if (claims.Contains(roleClaim)) { continue; } claims.Add(roleClaim); } } var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(claims), Expires = DateTime.UtcNow.Add(jwtSettings.TokenLifeTime), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; var token = tokenHandler.CreateToken(tokenDescriptor); var refreshToken = new RefreshToken() { JwtId = token.Id, UserId = user.Id, CreationDate = DateTime.UtcNow, ExpiryDate = DateTime.UtcNow.AddMonths(6) }; await context.RefreshTokens.AddAsync(refreshToken); await context.SaveChangesAsync(); return(new AuthenticationResult() { Success = true, Token = tokenHandler.WriteToken(token), RefreshToken = refreshToken.Token.ToString() }); }
public async Task <IList <string> > GetRolesFromUserAsync(SantapanUser user) { return(await userManager.GetRolesAsync(user)); }