I need to check if the code entered on resetpassword view is the same as the one sent in sms.
we are using repository pattern
i first generate the code in businesslogic
public class GenerateCodeBusiness
{
public string CreateRandomPassword(int PasswordLength)
{
string _allowedChars = "0123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ";
Random randNum = new Random();
char[] chars = new char[PasswordLength];
int allowedCharCount = _allowedChars.Length;
for (int i = 0; i < PasswordLength; i++)
{
chars[i] = _allowedChars[(int)((_allowedChars.Length) * randNum.NextDouble())];
}
return new string(chars);
}
}
}
then i send it using a phonereset method in my controller
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> PhoneReset(ForgotPasswordView model, string sms)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindByEmailAsync(model.Email);
if (user != null)
{
GenerateCodeBusiness gen = new GenerateCodeBusiness();
SendSmsBusiness objap = new SendSmsBusiness();
sms = "Your password reset code is " + gen.CreateRandomPassword(8);
objap.Send_SMS(model.cellNumber, sms);
await SignInAsync(user, isPersistent: false);
return RedirectToAction("ResetViaPhone", "Account");
}
else if (user == null)
{
ModelState.AddModelError("", "The user does not exist");
return View();
}
}
return View(model);
}
this is the resetpassowrd page controller method
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> ResetViaPhone(ResetViaPhoneView model)
{
if (ModelState.IsValid)
{
GenerateCodeBusiness gen = new GenerateCodeBusiness();
var user = await UserManager.FindByNameAsync(model.Email);
if (user == null && model.code != gen.CreateRandomPassword(8))
{
ModelState.AddModelError("", "No user found.");
return View();
}
IdentityResult result = await UserManager.ResetPasswordAsync(user.Id, model.code, model.New_Pass);
if (result.Succeeded)
{
return RedirectToAction("ResetPasswordConfirmation", "Account");
}
else
{
AddErrors(result);
return View();
}
}
return View(model);
}
this is my reset password view
plate.Model.ResetViaPhoneView
@{
ViewBag.Title = "Reset password";
Layout = "~/Views/shared/_BootstrapLayout.basic.cshtml";
}
@using (Html.BeginForm("ResetViaPhone", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<hr />
@Html.ValidationSummary("", new { @class = "text-danger" })
<div class="panel panel-default">
<div class="panel-heading " style="background-color: green "></div>
<div class="panel-body">
<div class="form-group">
@Html.LabelFor(model => model.code, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.code, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.Email, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.New_Pass, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.New_Pass, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Confirm_Pass, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.Confirm_Pass, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Confirm_Pass)
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>