0
I want to implement password encryption in user registration. I found here in stackoverflow this post teaching how to use cryptography, but I’m in doubt where to add the code.
This method should be used in the class or controller?
public string CalculateMD5Hash(string input)
{
// Calcular o Hash
MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
byte[] hash = md5.ComputeHash(inputBytes);
// Converter byte array para string hexadecimal
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("X2"));
}
return sb.ToString();
}
To use, where should I call?
seuModel.Senha = EncodePassword(senha);
People Controller, where register the user:
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Nome,Email,Senha")] Pessoas pessoas)
{
if (ModelState.IsValid)
{
db.Pessoas.Add(pessoas);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(pessoas);
}
In the user registration view I have the password field, so:
<div class="form-group col-sm-6">
@Html.LabelFor(model => model.Senha, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Senha, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Senha, "", new { @class = "text-danger" })
</div>
Do not mix the responsibilities. The password should already go
ciptografada
for yourmodel
make only the recording in the bank, that is, this method would be called in yourcontroller
.– Jorge.M
A suggestion would be to use Identity for your login system, it implements several security tools, if you have at the beginning of your project is very worth using
– Marcos Brinner