Beispiel #1
0
        public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, RepresentsNumberParticipants = 1
                };
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    try
                    {
                        await _newsletterSubscriptionService.SetSubscriptionAsync(model.Email, model.NewsletterSubscription);
                    }
                    catch (Exception e)
                    {
                        _logger.LogError("error subscribing to newsletter: {0}, {1}", e, user.Email);
                    }

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.Action(nameof(ConfirmEmail), "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
                    await _emailSender.SendEmailAsync(
                        model.Email,
                        _localizer["Confirm your account"],
                        $"{_localizer["Please confirm your account by clicking this link"]}: <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>{_localizer["link"]}</a>");

                    await _signInManager.SignInAsync(user, isPersistent : false);

                    _logger.LogInformation(3, "User created a new account with password.");
                    return(RedirectToLocal(returnUrl));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
        public async Task <IActionResult> NewsletterSubscription(IndexViewModel model)
        {
            var user = await GetCurrentUserAsync();

            if (user != null)
            {
                try
                {
                    await _newsletterSubscriptionService.SetSubscriptionAsync(user.Email, model.NewsletterSubscription, false);

                    StatusMessage = _localizer["Successfully subscribed to newsletter"];
                }
                catch (Exception e)
                {
                    _logger.LogError("error subscribing to newsletter: {0}, {1}", e, user.Email);
                }
            }
            return(RedirectToAction(nameof(Index)));
        }