Esempio n. 1
0
        public async Task <IServiceResult <User> > CreateUser(
            string email,
            string password,
            RoleType[] roles)
        {
            if (await EmailExists(email))
            {
                return(ServiceResult <User> .Error(
                           $"Email {email} is already taken"));
            }

            var roleNames = roles.Select(r => GetRoleName(r));

            foreach (var roleName in roleNames)
            {
                if (!(await _roleManager.RoleExistsAsync(roleName)))
                {
                    return(ServiceResult <User> .Error(
                               $"Role {roleName} does not exist"));
                }
            }

            var user = new User
            {
                //Id = Guid.NewGuid(),
                UserName           = email,
                NormalizedUserName =
                    new UpperInvariantLookupNormalizer()
                    .Normalize(email)
                    .ToUpperInvariant(),
                Email           = email,
                NormalizedEmail =
                    new UpperInvariantLookupNormalizer()
                    .Normalize(email)
                    .ToUpperInvariant(),
                EmailConfirmed = true,
                PasswordHash   = new PasswordHasher <User>().HashPassword(null, password),
            };

            user.SecurityStamp = _tokenGenerator.GenerateTokenFor(user);

            await _userManager.CreateAsync(user);

            await _userManager.AddToRolesAsync(user, roleNames);

            await _context.SaveChangesAsync();

            return(ServiceResult <User> .Success(user));
        }
Esempio n. 2
0
        public async Task <IServiceResult <UserSecurityStamp> > Login(string email, string password)
        {
            var user = await _userManager.FindByEmailAsync(email);

            if (user == null)
            {
                return(ServiceResult <UserSecurityStamp> .Error("User not found"));
            }

            var canSignIn = await _signInManager.CanSignInAsync(user);

            if (canSignIn)
            {
                var result =
                    await _signInManager.PasswordSignInAsync(email, password, true, false);

                if (result.Succeeded)
                {
                    var roles = await _userManager.GetRolesAsync(user);

                    string token = _tokenGenerator.GenerateTokenFor(user, roles);
                    user.SecurityStamp = token;

                    return(ServiceResult <UserSecurityStamp> .Success(new UserSecurityStamp
                    {
                        Email = user.Email,
                        SecurityStamp = user.SecurityStamp
                    }));
                }
            }
            return(ServiceResult <UserSecurityStamp> .Error("User cannot sign in"));
        }
Esempio n. 3
0
        private async Task SetSecurityStampAsync(User user, IEnumerable <string> roleNames)
        {
            string token = _tokenGenerator.GenerateTokenFor(user, roleNames.ToArray());

            user.SecurityStamp = token;
            await _context.SaveChangesAsync();
        }