public async Task <RegistrationResult> Register(string name, string surname, int age, string email, string password, string confirmPassword)
        {
            if (password != confirmPassword)
            {
                return(RegistrationResult.Fail);
            }
            var userWithProvidedEmailExists = await _customUserManager.UserExists(email);

            if (userWithProvidedEmailExists)
            {
                return(RegistrationResult.EmailAlreadyTaken);
            }

            var user           = new User(email, name, surname, age);
            var hashedPassword = _passwordHasher.HashPassword(user, password);

            user.SetPassword(hashedPassword);
            await _customUserManager.CreateUser(user);

            var roleExists = await _customRoleManager.RoleExists("Client");

            if (!roleExists)
            {
                await _customRoleManager.CreateRole(new Role("Client"));
            }

            await _customRoleManager.AddToRole(user, "Client");

            return(RegistrationResult.Success);
        }
コード例 #2
0
        public async Task <RegistrationResult> Register(string name, string email, string password,
                                                        string confirmPassword)
        {
            if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(email) && !string.IsNullOrEmpty(password) &&
                !string.IsNullOrEmpty(confirmPassword))
            {
                if (password != confirmPassword)
                {
                    return(RegistrationResult.Fail);
                }

                var userWithProvidedEmailExists = await _customUserManager.UserExists(email);

                if (userWithProvidedEmailExists)
                {
                    return(RegistrationResult.EmailAlreadyTaken);
                }

                var userId         = Guid.NewGuid();
                var user           = new User(userId, email, email, userId);
                var hashedPassword = _passwordHasher.HashPassword(user, password);
                user.SetHashedPassword(hashedPassword);
                var clientDetails = new Client(userId, name, 0);
                await _customUserManager.CreateUser(user);

                await _clientRepository.AddOne(clientDetails);

                var roleExists = await _customRoleManager.RoleExists("Client");

                if (!roleExists)
                {
                    await _customRoleManager.CreateRole(new Role("Client"));
                }

                await _customRoleManager.AddToRole(user, "Client");

                return(RegistrationResult.Success);
            }

            return(RegistrationResult.Fail);
        }