Пример #1
0
        public async Task ExecuteResultAsync(ActionContext context)
        {
            var factory = context.HttpContext.RequestServices.GetService <ITempDataDictionaryFactory>();

            var tempData = factory.GetTempData(context.HttpContext);

            tempData["_statusMessage"] = new StatusMessageWithType(Title, Body, Type);

            await Result.ExecuteResultAsync(context);
        }
Пример #2
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (ModelState.IsValid)
            {
                var user = new User
                {
                    UserName = Input.Email, Email = Input.Email,
                    Created  = DateTime.Now, Updated = DateTime.Now
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    result = await _userManager.AddToRoleAsync(user, "User");

                    result = await _userManager.AddToRoleAsync(user, "Staff");

                    var isFirstUser = await _userManager.Users.CountAsync() == 1;

                    if (isFirstUser)
                    {
                        result = await _userManager.AddToRoleAsync(user, "Admin");

                        result = await _userManager.AddToRoleAsync(user, "Moderator");

                        var activationToken = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                        await _userManager.ConfirmEmailAsync(user, activationToken);
                    }

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

                            return(LocalRedirect(returnUrl));
                        }

                        var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                        var callbackUrl = Url.Page(
                            "/Account/ConfirmEmail",
                            pageHandler: null,
                            values: new { userId = user.Id, code = code },
                            protocol: Request.Scheme);

                        await _emailHelper.SendAdminConfirmationEmail(Input.Email, callbackUrl);

                        StatusMessage = new StatusMessageWithType("Success",
                                                                  "Confirmation email sent. Please check your email.",
                                                                  "info");
                        return(Page());
                    }
                }

                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }