Implement Forgot Password using AngularJS and Entity Framework in ASP.Net MVC

Last Reply 10 months ago By dharmendr

Posted 10 months ago

Hi,

Based upon below Forum i am going to implement forget password funtionality using anjularjs and MVC with linq queries.

Implement Forgot Password using AngularJS in ASP.Net

Could you please help me.

Posted 10 months ago Modified on 10 months ago

Hi mahesh213,

Using the below article i have created the example.

Implement Forgot Password (Password Recovery) and send password in Email in ASP.Net

Now please take its reference and correct your code.

Namespaces

using System.Net;
using System.Net.Mail;

Controller

public class HomeController : Controller
{
    // GET: /Home/
    public ActionResult Index()
    {
        return View();
    }

    public JsonResult SendEmail(string email)
    {
        string message = "This email address does not match our records.";
        UserEntities entities = new UserEntities();
        User user = entities.Users.Where(x => x.Email == email).FirstOrDefault();
        if (user != null)
        {
            string username = user.Username;
            string password = user.Password;
            if (!string.IsNullOrEmpty(password))
            {
                MailMessage mm = new MailMessage("sender@gmail.com", email);
                mm.Subject = "Password Recovery";
                mm.Body = string.Format("Hi {0},<br /><br />Your password is {1}.<br /><br />Thank You.", username, password);
                mm.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.EnableSsl = true;
                NetworkCredential NetworkCred = new NetworkCredential();
                NetworkCred.UserName = "sender@gmail.com";
                NetworkCred.Password = "<Password>";
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = NetworkCred;
                smtp.Port = 587;
                smtp.Send(mm);
                message = "Password has been sent to your email address.";
            }
        }

        return Json(message, JsonRequestBehavior.AllowGet);
    }
}

View

<html>
<head>
    <title>Index</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js"></script>
    <script type="text/javascript">
        var app = angular.module('MyApp', []);
        app.controller("MyController", function ($scope, $http, $window) {
            $scope.SendEmail = function () {
                $http.get("/Home/SendEmail/", { params: { email: $scope.Email} })
                .then(function (response) {
                    $scope.Message = response.data;
                    if (response.data == 'This email address does not match our records.') {
                        $scope.Color = "Red";
                    }
                    else if (response.data == 'Password has been sent to your email address.') {
                        $scope.Color = "Green";
                    }
                    $scope.Email = "";
                }).then(function (response) {
                    $scope.Message = response.responseText;
                });
            }
        });
    </script>
</head>
<body>
    <div ng-app="MyApp" ng-controller="MyController">
        <input type="text" name="email" ng-model="Email" />
        <input type="button" name="name" value="Send" ng-click="SendEmail()" />
        <br />
        <span ng-style="{color:Color}" ng-bind="Message"></span>
    </div>
</body>
</html>

Screenshot