예제 #1
0
        // --------------

        async Task <ICommandResultBase> InstallInternalAsync()
        {
            // Our result
            var result = new CommandResultBase();

            foreach (var username in Usernames)
            {
                var userNAme = username;
                var email    = username + "@example.com";
                var password = "******";

                var newUserResult = await _platoUserManager.CreateAsync(new User()
                {
                    UserName    = userNAme,
                    Email       = email,
                    Password    = password,
                    DisplayName = userNAme
                });
            }

            return(result.Success());
        }
예제 #2
0
        public async Task <IActionResult> CreatePost(EditUserViewModel model)
        {
            // Ensure we have permission
            if (!await _authorizationService.AuthorizeAsync(User,
                                                            Permissions.AddUsers))
            {
                return(Unauthorized());
            }

            // Build user
            var user = new User()
            {
                DisplayName = model.DisplayName,
                UserName    = model.UserName,
                Email       = model.Email,
                Password    = model.Password,
                Biography   = model.Biography,
                Location    = model.Location,
                Signature   = model.Signature,
                Url         = model.Url
            };

            // Validate model state within view providers
            var valid = await _viewProvider.IsModelStateValidAsync(user, this);

            // Ensure password fields match
            if (model.Password != model.PasswordConfirmation)
            {
                ViewData.ModelState.AddModelError(nameof(model.PasswordConfirmation), "Password and Password Confirmation do not match");
                valid = false;
            }

            // Validate model state within all view providers
            if (valid)
            {
                // Get composed model from all involved view providers
                user = await _viewProvider.ComposeModelAsync(user, this);

                // Create the composed type
                var result = await _platoUserManager.CreateAsync(
                    user.UserName,
                    user.DisplayName,
                    user.Email,
                    user.Password);

                if (result.Succeeded)
                {
                    // Execute view providers ProvideUpdateAsync method
                    await _viewProvider.ProvideUpdateAsync(result.Response, this);

                    // Everything was OK
                    _alerter.Success(T["User Created Successfully!"]);

                    // Redirect back to edit user
                    return(RedirectToAction(nameof(Edit), new RouteValueDictionary()
                    {
                        ["id"] = result.Response.Id.ToString()
                    }));
                }
                else
                {
                    // Errors that may have occurred whilst creating the entity
                    foreach (var error in result.Errors)
                    {
                        ViewData.ModelState.AddModelError(string.Empty, error.Description);
                    }
                }
            }

            return(await Create());
        }
예제 #3
0
        public async Task <IActionResult> ExternalLoginConfirmation(ExternalLoginViewModel model, string returnUrl = null, string loginProvider = null)
        {
            if (!model.IsExistingUser && !_siteOptions.AllowUserRegistration)
            {
                _logger.LogInformation("Site does not allow user registration.");
                return(NotFound());
            }

            ViewData["ReturnUrl"]     = returnUrl;
            ViewData["LoginProvider"] = loginProvider;

            if (ModelState.IsValid)
            {
                User user = null;
                var  info = await _signInManager.GetExternalLoginInfoAsync();

                if (info == null)
                {
                    throw new ApplicationException("Error loading external login information during confirmation.");
                }

                if (!model.IsExistingUser)
                {
                    var result = await _platoUserManager.CreateAsync(new User()
                    {
                        UserName       = model.UserName,
                        Email          = model.Email,
                        EmailConfirmed = true,
                        RoleNames      = new[]
                        {
                            DefaultRoles.Member
                        }
                    }, model.Password);

                    if (result.Succeeded)
                    {
                        user = result.Response;
                    }
                    else
                    {
                        foreach (var error in result.Errors)
                        {
                            ModelState.AddModelError(error.Description, error.Description);
                        }
                    }
                    _logger.LogInformation(3, "User created an account with password.");
                }
                else
                {
                    user = await _userManager.FindByNameAsync(model.UserName);
                }

                if (user != null)
                {
                    var signInResult = await _signInManager.CheckPasswordSignInAsync(user, model.Password, false);

                    if (!signInResult.Succeeded)
                    {
                        user = null;
                        ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                    }
                    else
                    {
                        var identityResult = await _signInManager.UserManager.AddLoginAsync(user, new UserLoginInfo(info.LoginProvider, info.ProviderKey, info.ProviderDisplayName));

                        if (identityResult.Succeeded)
                        {
                            await _signInManager.SignInAsync(user, isPersistent : false);

                            _logger.LogInformation(3, "User account linked to {Name} provider.", info.LoginProvider);
                            return(RedirectToLocal(returnUrl));
                        }
                        //AddErrors(identityResult);
                    }
                }
            }
            return(View(nameof(ExternalLogin), model));
        }
예제 #4
0
        public async Task <IActionResult> Register(RegisterViewModel viewModel, string returnUrl = null)
        {
            // Persist returnUrl
            ViewData["ReturnUrl"] = returnUrl;

            // Build model for view providers
            var registration = new UserRegistration()
            {
                UserName        = viewModel.UserName,
                Email           = viewModel.Email,
                Password        = viewModel.Password,
                ConfirmPassword = viewModel.ConfirmPassword
            };

            // Validate model state within all involved view providers
            if (await _registerViewProvider.IsModelStateValidAsync(registration, this))
            {
                // Get composed type from all involved view providers
                registration = await _registerViewProvider.ComposeModelAsync(registration, this);

                // Create the user from composed type
                var result = await _platoUserManager.CreateAsync(
                    registration.UserName,
                    registration.Email,
                    registration.Password);

                //var result = await _userManager.CreateAsync(registerViewModel, registerViewModel.Password);
                if (result.Succeeded)
                {
                    // Indicate new flag to allow optional update
                    // on first creation within any involved view provider
                    registration.IsNewUser = true;

                    // Execute ProvideUpdateAsync
                    await _registerViewProvider.ProvideUpdateAsync(registration, this);

                    // Success - Redirect to confirmation page
                    return(RedirectToAction(nameof(RegisterConfirmation)));
                }
                else
                {
                    // Report errors that may have occurred whilst creating the user
                    foreach (var error in result.Errors)
                    {
                        ViewData.ModelState.AddModelError(string.Empty, error.Description);
                    }
                }
            }

            // if we reach this point some view model validation
            // failed within a view provider, display model state errors
            foreach (var modelState in ViewData.ModelState.Values)
            {
                foreach (var error in modelState.Errors)
                {
                    //_alerter.Danger(T[error.ErrorMessage]);
                }
            }

            return(await Register(returnUrl));
        }