2
I have a way of Email which generates a link through HTTP request, which will send to the user’s email for exchange. But for testing I fixed in this URL the user ID, but for security reasons I have to encrypt it. How to perform this procedure ?
Follows code
Metodo Confirmaenvio
public IActionResult ConfirmarEnvio(Clientes objLogin)
{
try
{
var link = HttpContext.Request.Host.Value;
Email objEmail = new Email(_config);
objEmail.CliCodigo = objLogin.CliCodigo;
objEmail.CliEmail = objLogin.CliEmail;
objEmail.link = link;
objEmail.EnviarEmail();
return View("Login");
}
catch (Exception ex)
{
throw ex;
}
}
Part of the method that generates the link:
//From Address
string FromAddress = _config.GetValue<string>("From");
string FromAdressTitle = "TesteEnvio";
//To Address
string ToAddress = CliEmail;
string ToAdressTitle = "Microsoft ASP.NET Core";
string Subject = "Redefinição de senha";//Assunto,
StringBuilder BodyContent = new StringBuilder();
BodyContent.Append ("Prezado(a) para redefinir sua senha, por favor clique no link abaixo.");
BodyContent.Append ("<br/>");
BodyContent.Append ("Link: http://" + link + "/Accounts/RedefinicaoSenha?id=" + CliCodigo +"");
Just out of curiosity, why encrypting the user ID will bring some security?
– Jéf Bueno
Because if I paste that URL this way, it will open the password change view and so anyone could change the password without any impediment. See, for the user to reach this point of change it is necessary to make a request on the Login screen, where automatically will load a modal already with the email related to the user’s ID locked, thus preventing the change of email by third parties.
– Igor Carreiro
Man, the way I see it, cryptography’s not gonna do you much good on this case. Anyone with this link, even if encrypted, worldwide could access your application. I think you could work with a temporary link, you know? 10 minutes or less. Create a key and send that key which can be in md5 or sha1, etc. Send the key in the email link and relate this key to the user who will exchange the password. Understand? Put a short but sufficient time. This way, your security will be a little better than just encrypting the user id.
– DiegoSantos
@And you think encrypting the user ID is the best solution for that? Why not generate a single hash and send it to the user’s email?
– Jéf Bueno
@LINQ as it is a new feature for me, I’m open to opinions, could exemplify how to follow this path ?
– Igor Carreiro
@Diegosantos I agree with what you said, but this process of validation of password exchange by link is new for me! So I’m still analyzing the best routes to follow with good 'manners' to develop this logic
– Igor Carreiro
I understand @Igorcarreiro, and I think you’re right! Well I point out this way that I said because I have done so a few times and it is not such an honorable process if you already have a certain knowledge in the database, mainly. Examples of how to generate the key, you can generate one in sql server with a select newid(), for example. In C#, you can generate it with a GUID. It’s pretty simple... But follow what you find peaceful for you! Good luck...
– DiegoSantos
I agree with @Diegosantos, but I create a link that can be used only once, the key I Gero using New Guid() and then only pass to Base64, simple, unique, safe. in the database I place this "key" in a table, which has the user id reference, and good. I mark this link as accessed, and then it’s gone, it’s in the bank only for history.
– Zorkind