Esempio n. 1
0
        public async Task <UserCreationResultServiceModel> CreateUserAsync(User user, string password)
        {
            User existingUser = await this.FindUserByUserNameOrEmailAsync(user.UserName.CleanLoginEmail());

            UserCreationResultServiceModel result = new UserCreationResultServiceModel(ErrorResources.ErrorOnUserCreation);

            if (existingUser == null)
            {
                var resuser = await this.userManager.CreateAsync(user, password);

                if (resuser.Succeeded)
                {
                    var created = this.userManager.FindByName(user.UserName.CleanLoginEmail());
                    result = new UserCreationResultServiceModel(created);
                }
                else
                {
                    result = new UserCreationResultServiceModel(string.Join(", ", resuser.Errors.Select(s => "\n " + s)));
                }
            }
            else
            {
                result = new UserCreationResultServiceModel(ErrorResources.ErrorOnUserCreation_UserAlreadyExist);
            }
            return(result);
        }
Esempio n. 2
0
        public async Task <UserCreationResultServiceModel> CreateUserWithMainRoleAsync(User user, string password, string mainRoleName = "")
        {
            User existingUser = await this.FindUserByUserNameOrEmailAsync(user.UserName.CleanLoginEmail());

            UserCreationResultServiceModel result = new UserCreationResultServiceModel(ErrorResources.ErrorOnUserCreation);

            try
            {
                if (existingUser == null)
                {
                    var resuser = await this.userManager.CreateAsync(user, password);

                    if (resuser.Succeeded)
                    {
                        if (!string.IsNullOrWhiteSpace(mainRoleName))
                        {
                            this.userManager.AddToRole(user.Id, mainRoleName);
                        }

                        var created = this.userManager.FindByName(user.UserName.CleanLoginEmail());
                        result = new UserCreationResultServiceModel(created);
                    }
                    else
                    {
                        result = new UserCreationResultServiceModel(string.Join(", ", resuser.Errors.Select(s => "\n " + s)));
                    }
                }
                else
                {
                    result = new UserCreationResultServiceModel(ErrorResources.ErrorOnUserCreation_UserAlreadyExist);
                }
            }
            catch (Exception ex)
            {
                result.Succeeded   = false;
                result.FailMessage = ex.Message;
            }

            return(result);
        }