Exemplo n.º 1
0
        public async Task <IActionResult> Mail(string id)
        {
            string email = id;

            var message = new MimeMessage();
            var builder = new BodyBuilder
            {
                TextBody = "Hello. It works."
            };

            message.Body    = builder.ToMessageBody();
            message.Subject = "Test email";
            message.To.Add(new MailboxAddress(name: null, address: email));
            message.From.Add(SmtpClient.SystemFromAddress);
            try
            {
                await SmtpClient.SendMessageAsync(message);

                return(Content("OK. Check your inbox."));
            }
            catch (Exception ex)
            {
                return(Content($"{ex.Message}\r\n\r\n{ex.StackTrace}"));
            }
        }
        public async Task <IActionResult> Forgot(ForgotViewModel model)
        {
            //validate username exists
            //validate user is active
            //encode reset token
            //mail reset token

            var user = await DbContext.Users.Where(x => x.Name == model.Username).FirstOrDefaultAsync();

            if (user == null)
            {
                ModelState.AddModelError("", "Please register before attempting to log in.");
                ModelState.AddModelError("Username", $"\"{model.Username}\" is not registered.");
                return(View(model));
            }

            if (!user.Active)
            {
                ModelState.AddModelError("", $"\"{model.Username}\" is disabled. Please contact the administrator.");
                ModelState.AddModelError("Username", $"\"{ model.Username}\" is disabled.");
                return(View(model));
            }

            var token     = EncodeResetToken(user);
            var cryptoken = EncryptToken(token);

            var resetLink = Url.ActionLink("Reset", "Account", new { id = cryptoken });

            var message = new MimeMessage();

            message.From.Add(SmtpClient.SystemFromAddress);
            message.To.Add(new MailboxAddress((string?)null, user.Email));
            message.Subject = "Password Reset Requested";

            var builder = new BodyBuilder
            {
                //TODO: message tempate as txt and html part resource
                TextBody = "Follow the link below to reset your password in the TBD system. " +
                           $"This reset link will expire {TokenValidMinutes} minutes after you requested it. " +
                           "If you do not wish to reset your password, you can safely ignore this message.\n\n" +
                           $"<{resetLink}>",

                HtmlBody = "<p>The link below will allow you to reset your password in the TBD system.</p>" +
                           $"<p><a href={resetLink}>Follow this link to reset your password. It will " +
                           $"expire {TokenValidMinutes} minutes after you requested it.</a></p>" +
                           "<p>If you do not wish to reset your password, you can safely ignore this message.</p>"
            };

            message.Body = builder.ToMessageBody();

            try
            {
                await SmtpClient.SendMessageAsync(message);

                ViewBag.Message = $"We have sent you an email from {SmtpClient.SystemFromAddress.Address} that contains a link to reset your email. " +
                                  $"The link will expire in {TokenValidMinutes} minutes.";
            }
            catch (Exception e)
            {
                Logger.LogError(e, "Error sending email.");
                ModelState.AddModelError("", e.Message);
            }

            return(View(model));
        }