We provide IT Services

Register Now Click On

Read More Tutorials Click On

Post Page Advertisement [Top]


Forgot Password In MVC Application:


In this article, we Provide how-to forgot password send on the email page in MVC.  if the user forgets the password. And Read continue this article. so you can create easily Forgot password in your MVC application.

controller Code:-

public ActionResult ForgotPassword(string clientid, string RegisterdEmailID)
        {
            DataTable dt = new DataTable();
            string msg = "";

            ForgotPassword _passwordforgot = new ForgotPassword();

            dt = _passwordforgot.MatchEmailId(Clientid, RegisterdEmailID);

            if (dt.Rows.Count == 0)
            {
                return Json(new
                {
                    result = Newtonsoft.Json.JsonConvert.SerializeObject(msg)
                }, JsonRequestBehavior.AllowGet);
            }
            string COMPANY_CODE = "";
            string EMAIL = "";
            foreach (DataRow row in dt.Rows)
            {
                COMPANY_CODE = row["COMPANY_CODE"].ToString();
                EMAIL = row["EMAIL"].ToString();
            }
            MailMessage mm = new MailMessage();

           // mm.From  = new MailAddress("example@gmail.com");

            var varifyUrl = "<a href='" + Url.Action("Create", "Home", new { email = EMAIL, code =                 COMPANY_CODE }, "http") + "'>Reset Password</a>";
            var fromMail = new MailAddress("youremail@gmail.com");
            var toMail = new MailAddress(EMAIL);
            var frontEmailPassowrd = "yourpassword";
            string subject = "Your account is successfull created";
            string body = "<br/><br/>We are excited to tell you that your account is" +
            "successfully created. Please click on the below link to verify your account" + " <br/><br/>               <a  href='" + varifyUrl + "'>" + varifyUrl + "</a> ";
            var smtp = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(fromMail.Address, frontEmailPassowrd)

            };
            using (var message = new MailMessage(fromMail, toMail)
            {
                Subject = subject,
                Body = body,
                IsBodyHtml = true
            })
                smtp.Send(message);

            return View();
        }

Model Code:-

public DataTable MatchEmailId(string COMPANY_CODE, string EMAIL)
        {
            try
            {
                using (SqlConnection con = new     SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
                {
                    string query = "SELECT COMPANY_CODE,EMAIL  FROM TBLREG_COMPANY WHERE COMPANY_CODE = '" + COMPANY_CODE + "' AND EMAIL = '" + EMAIL + "' ";
                    SqlCommand cmd = new SqlCommand(query, con);
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    da.Fill(dt);

                    if (dt == null || dt.Rows.Count <= 0)
                    {
                        dt.Columns.Add("Status");
                    }
                    return dt;
                }
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write(ex.Message);
                return null;
            }
        }

.js Ajax Code:-

$(document).ready(function () {
    $("#btnAEForgotPassword").on("click", function () {
        ForgotPassword();
    });
});


function ForgotPassword() {
    debugger
    var Clientid = $("#txtAEClientId").val();
    var RegisterdEmailID = $("#txtAERegisterdEmailID").val();

    $.ajax({
        url: $("#btnAEForgotPassword").attr('data-saveurl'),
        type: 'post',
        cache: false,
        data: { "Clientid": Clientid, "RegisterdEmailID": RegisterdEmailID },
        dataType: 'json',
        beforeSend: function () {
            $("#contributionSpinner").show();
        },
        success: function (data) {
            var objdata = $.parseJSON(data.result);
            if (objdata != null) {
                if (objdata.length == 0) {
                    swal("Not Valid RegistrtionId");
                }
            }
        },
        error: function (jqXHR, exception) {
            swal(" Valid RegistrtionId", getErrorMessage(jqXHR, exception), "error");
        }
    });
}

.Cshtml Code:-

<div class="modal fade" id="dvmdlForgotPassword" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog modal-dialog-centered modal-md" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLongTitle">
                    <i class="fa fa-key"></i> Retrieve your password
                    <small class="mandatory-title">* Indicates MandatoryFields</small>
                </h5>
                <button type="button" class="close frmclose" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            </div>
            <form method="post" class="form-horizontal" id="frmAEForgotPassword">
                <div class="modal-body">
                    <div class="row">
                        <div class="col-xs-12 col-sm-12 col-md-12">
                            <p style="margin-bottom: 10px;">Enter email address associated with Wizgem ERP account.</p>
                            <div class="form-group row">
                                <div class="col-sm-12">
                                    <div class="input-group">
                                        <div class="input-group-prepend">
                                            <span class="input-group-text">
                                                <span class="fafa-user"></span>
                                            </span>
                                        </div>
                                        <input type="text" class="form-control form-control-sm" name="txtAEClientId" id="txtAEClientId" placeholder="Enter your client Id" />
                                    </div>
                                </div>
                                <div class="col-sm-12">
                                    <div class="input-group">
                                        <div class="input-group-prepend">
                                            <span class="input-group-text">
                                                <span class="fafa-envelope"></span>
                                            </span>
                                        </div>
                                        <input type="email" autocomplete="off" maxlength="50" class="form-control form-control-sm" name="txtAERegisterdEmailID" id="txtAERegisterdEmailID" placeholder="Enteryour email address">
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer modal-footer-custom text-right">
                    <button type="submit" class="btn btn-radius lightGray" id="btnAEForgotPassword" data-saveurl="@Url.Action("ForgotPassword", "Home")"><i class="fasfa-save"></i> GO</button>
                    <button type="button" class="btn btn-radius btn-danger frmclose" id="btnclose" data-dismiss="modal"><i class="fa fa-times"></i>Close</button>
                </div>
            </form>
        </div>
    </div>
</div>

No comments:

Post a Comment

| Designed by Rockprogrammer