Implement Forgot Password using AngularJS in ASP.Net

Last Reply 5 months ago By dharmendr

Posted 5 months ago

How to implement forgot password in angularjs in

Posted 5 months ago

Hi rani,

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.


<script type="text/javascript" src=""></script>
<script type="text/javascript">
    var app = angular.module('MyApp', []);
    app.controller("MyController", function ($scope, $http, $window) {
        $scope.SendEmail = function () {
            $"Default.aspx/SendEmail", JSON.stringify({ email: $scope.Email }), { headers: { 'Content-Type': 'application/json'} })
            .then(function (response) {
                $scope.Message =;
            }, function error(response) {
                $scope.Message = response.responseText;
<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 style="color: Red;" ng-bind="Message"></span>



using System.Configuration;
using System.Data.SqlClient;
using System.Net;
using System.Net.Mail;
using System.Web.Services;


Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Net
Imports System.Net.Mail
Imports System.Web.Services



public static string SendEmail(string email)
    string username = string.Empty;
    string password = string.Empty;
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
        using (SqlCommand cmd = new SqlCommand("SELECT Username, [Password] FROM Users WHERE Email = @Email"))
            cmd.Parameters.AddWithValue("@Email", email);
            cmd.Connection = con;
            using (SqlDataReader sdr = cmd.ExecuteReader())
                if (sdr.Read())
                    username = sdr["Username"].ToString();
                    password = sdr["Password"].ToString();
    if (!string.IsNullOrEmpty(password))
        MailMessage mm = new MailMessage("", 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.EnableSsl = true;
        NetworkCredential NetworkCred = new NetworkCredential();
        NetworkCred.UserName = "";
        NetworkCred.Password = "<Password>";
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = NetworkCred;
        smtp.Port = 587;

        return "Password has been sent to your email address.";
        return "This email address does not match our records.";


Public Shared Function SendEmail(ByVal email As String) As String
    Dim username As String = String.Empty
    Dim password As String = String.Empty
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(constr)
        Using cmd As SqlCommand = New SqlCommand("SELECT Username, [Password] FROM Users WHERE Email = @Email")
            cmd.Parameters.AddWithValue("@Email", email)
            cmd.Connection = con
            Using sdr As SqlDataReader = cmd.ExecuteReader()
                If sdr.Read() Then
                    username = sdr("Username").ToString()
                    password = sdr("Password").ToString()
                End If
            End Using
        End Using
    End Using

    If Not String.IsNullOrEmpty(password) Then
        Dim mm As MailMessage = New MailMessage("", 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
        Dim smtp As SmtpClient = New SmtpClient()
        smtp.Host = ""
        smtp.EnableSsl = True
        Dim NetworkCred As NetworkCredential = New NetworkCredential()
        NetworkCred.UserName = ""
        NetworkCred.Password = "<Password>"
        smtp.UseDefaultCredentials = True
        smtp.Credentials = NetworkCred
        smtp.Port = 587
        Return "Password has been sent to your email address."
        Return "This email address does not match our records."
    End If
End Function