Esempio n. 1
0
        public async Task <ActionResult> SendLoginEmail(ForgotPasswordViewModel vm)
        {
            if (ModelState.IsValid)
            {
                //Generate a new random password and change the password for this user
                string      tempPassword = Membership.GeneratePassword(8, 0);
                LoginResult updateResult = await loginManager.ChangePasswordIfForgotten(vm.Email, tempPassword);

                if (updateResult.Success)
                {
                    //Send Email with new Password

                    // Create the email object first, then add the properties.
                    SendGridMessage myMessage = new SendGridMessage();
                    myMessage.AddTo(vm.Email);
                    myMessage.From    = new MailAddress("*****@*****.**", "Connect and Ride");
                    myMessage.Subject = "Connect and Ride Login";
                    myMessage.Text    = "Please use the link below to login using the temporary password:\n\n" + tempPassword + "\n\nhttp://www.connectandride.com\n\nYou may change your password once you login.\n\n"
                                        + "Thank you,\n\nThe Battelle research team";

                    // Create credentials, specifying your user name and password.
                    var credentials = new NetworkCredential(SENDGRID_USERNAME, SENDGRID_PASSWORD);

                    // Create an Web transport for sending email.
                    var transportWeb = new Web(credentials);

                    // Send the email.
                    transportWeb.Deliver(myMessage);
                }
                else
                {
                    ModelState.AddModelError("Email", updateResult.ErrorString);
                    return(View("ForgotPassword", vm));
                }

                return(View(vm));
            }

            return(View("ForgotPassword", vm));
        }