Ejemplo n.º 1
0
        public async Task <IActionResult> Create(CreateUserViewModel newUser)
        {
            if (ModelState.IsValid)
            {
                var user = await this._userManager.FindByEmailAsync(newUser.Email);

                if (user == null)
                {
                    var userToCreate = default(MoxUser);

                    if (this._identityOptions?.DefaultUserType != null)
                    {
                        userToCreate = (MoxUser)Activator.CreateInstance(this._identityOptions.DefaultUserType);
                    }
                    else
                    {
                        userToCreate = new MoxUserBaseImpl();
                    }

                    userToCreate.UserName = newUser.Email;
                    userToCreate.Email    = newUser.Email;
                    userToCreate.Name     = newUser.Name;

                    var createUserResult = await this._userManager.CreateAsync(userToCreate, newUser.Password);

                    if (!createUserResult.Succeeded)
                    {
                        foreach (var error in createUserResult.Errors)
                        {
                            ModelState.AddModelError(string.Empty, error.Description);
                        }
                        return(View(newUser));
                    }

                    var addRoleResult = await this._userManager.AddToRolesAsync(userToCreate, newUser.Roles.Where(x => x.Checked).Select(x => x.Name));

                    if (!addRoleResult.Succeeded)
                    {
                        foreach (var error in addRoleResult.Errors)
                        {
                            ModelState.AddModelError(string.Empty, error.Description);
                        }
                        return(View(newUser));
                    }

                    return(RedirectToAction("Index"));
                }
                else
                {
                    ModelState.AddModelError(string.Empty, this._localizer["E-post addressen är upptagen!"]);
                }
            }

            return(View(newUser));
        }
Ejemplo n.º 2
0
        private async Task <string> EnsureUser(string testUserPw, string UserName)
        {
            var user = await this._userManager.FindByNameAsync(UserName);

            if (user == null)
            {
                user = new MoxUserBaseImpl {
                    UserName = UserName, Email = UserName, Name = "Back Door"
                };
                await this._userManager.CreateAsync(user, testUserPw);
            }

            return(user.Id);
        }