コード例 #1
0
        private async Task SendEmailConfirmation(IdentityUser user, string returnUrl = null)
        {
            var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

            var redirectUri = returnUrl != null
                ? new Uri(HttpUtility.ParseQueryString(Url.Content(returnUrl)).Get("redirect_uri"))
                              .GetLeftPart(UriPartial.Authority)
                : null;

            var callbackUrl = Url.Action(
                action: nameof(ConfirmEmail),
                controller: "Account",
                values: new { userId = user.Id, code, redirectUri },
                protocol: Request.Scheme);

            var outgoingEmail = new OutgoingEmail
            {
                To       = user.Email,
                From     = _transactionalTemplateConfiguration.FromEmail,
                Template = new Template
                {
                    Id            = _transactionalTemplateConfiguration.RegistrationTemplate,
                    Substitutions = new Dictionary <string, string>
                    {
                        { _transactionalTemplateConfiguration.DynamicUserNameKey, user.UserName },
                        { _transactionalTemplateConfiguration.DynamicLinkKey, callbackUrl }
                    },
                    IsDynamic = true
                }
            };

            await _emailSender.SendEmailAsync(outgoingEmail);
        }
コード例 #2
0
        public async Task <IActionResult> Registration(ApplicationUser user)
        {
            if (!ModelState.IsValid)
            {
                return(View(user));
            }

            var existingUser = await _userManager.FindByEmailAsync(user.Email);

            if (existingUser != null)
            {
                return(await SendSecurityViolationMessageAndReturnView(existingUser));
            }

            IdentityResult result = await _userManager.CreateAsync(user, user.Password);

            if (!result.Succeeded)
            {
                return(AddErrorsAndReturnToRegistrationView(user, result));
            }

            var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);

            var confirmEmailLink = Url.Action(nameof(ConfirmEmail), "Account", new { token, user.Email }, Request.Scheme);

            var message = new Message(new string[] { user.Email }, "Confirmation email link", confirmEmailLink);
            await _sendEmail.SendEmailAsync(message);

            await _userManager.AddToRoleAsync(user, "RegisteredUser");

            return(RedirectToAction(nameof(SuccessRegistration)));
        }
コード例 #3
0
        public async Task <bool> SendEmailAsync(string emailAddress, string emailBody,
                                                string emailSubject, bool isBodyHtml, bool replyExpected)
        {
            var successfullySent = false;

            if (emailAddress == null)
            {
                return(false);
            }
            try
            {
                await _sendEmail.SendEmailAsync(new EmailDto
                {
                    Body          = emailBody,
                    IsBodyHtml    = isBodyHtml,
                    ReplyExpected = replyExpected,
                    Subject       = emailSubject,
                    ToList        = new List <string> {
                        emailAddress
                    }
                });

                successfullySent = true;
            }
            catch (Exception ex)
            {
                // TODO: log the exception
            }

            return(successfullySent);
        }
コード例 #4
0
        public async Task <IActionResult> Index()
        {
            var message = new Message(new string[] { "*****@*****.**" }, "Test email", "This is the content from our email.");
            await _sendEmail.SendEmailAsync(message);

            var xx = await _repositoryWrapper.ImageRepository.GetAllImages();

            return(View(await _repositoryWrapper.ImageRepository.GetAllImages()));
        }
コード例 #5
0
        public async Task SendInvite([FromQuery] string userEmail)
        {
            var     projectState = _httpContextAccessor.HttpContext.Session.GetObjectFromJson <ProjectState>("ProjectState");
            int     projectID    = projectState.ProjectID;
            string  currentUser  = tasksRepository.GetLoggedInUser();
            Project project      = tasksRepository.GetProject(projectID);

            var    acceptInviteLink = Url.Action("AcceptInvite", "Home", new { projectID }, Request.Scheme);
            var    loginLink        = Url.Action("Login", "Account", new { returnUrl = acceptInviteLink }, Request.Scheme);
            string recipientBody    = $"You have been invited to join  {currentUser}'s Project: {project.ProjectName}. <br> Click <a href=\"{loginLink}\">here</a> to login and accept the invite.";

            await _emailSender.SendEmailAsync(userEmail, "Project Invite", recipientBody);

            string senderBody = $"Your invite has been sent to  + {userEmail}";

            string currentUserEmail = (await _userManager.FindByIdAsync(User.FindFirstValue(ClaimTypes.NameIdentifier))).Email;
            await _emailSender.SendEmailAsync(currentUserEmail, "Project Invite Sent", senderBody);
        }
コード例 #6
0
        public async Task <IActionResult> ForgotPassword(ForgotPasswordViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await userManager.FindByEmailAsync(model.Email);

                if (user != null /*&& await userManager.IsEmailConfirmedAsync(user)*/)
                {
                    var token = await userManager.GeneratePasswordResetTokenAsync(user);

                    var passwordResetLink = Url.Action("ResetPassword", "Account", new { email = model.Email, token }, Request.Scheme);

                    string emailBody = "Click this link to reset your password: \n <a href=\"" + passwordResetLink + "\">Reset Password</a>";
                    await _emailSender.SendEmailAsync(model.Email, "Reset Password Link", emailBody);

                    return(View("ForgotPasswordConfirmation"));
                }
                return(View("ForgotPasswordConfirmation"));
            }
            return(View(model));
        }