De la Seguridad del acceso a datos en aplicaciones ASP ... - Willy .Net
De la Seguridad del acceso a datos en aplicaciones ASP ... - Willy .Net
De la Seguridad del acceso a datos en aplicaciones ASP ... - Willy .Net
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Almac<strong>en</strong>e valores hash de contraseña unidireccionales (con un valor salt<br />
aleatorio).<br />
Evite <strong>la</strong> inyección SQL al validar cred<strong>en</strong>ciales de usuario.<br />
Almac<strong>en</strong>ar valores hash de contraseña unidireccionales<br />
(con salt)<br />
Las <strong>aplicaciones</strong> Web que utilizan <strong>la</strong> aut<strong>en</strong>ticación mediante Formu<strong>la</strong>rios necesitan a<br />
m<strong>en</strong>udo almac<strong>en</strong>ar <strong>la</strong>s cred<strong>en</strong>ciales de usuario (incluidas <strong>la</strong>s contraseñas) <strong>en</strong> una<br />
base de <strong>datos</strong>. Por motivos de seguridad, no debería almac<strong>en</strong>ar contraseñas (texto<br />
sin cifrar o cifrado) <strong>en</strong> <strong>la</strong> base de <strong>datos</strong>.<br />
<strong>De</strong>bería evitar almac<strong>en</strong>ar contraseñas cifradas porque eso aum<strong>en</strong>ta los problemas<br />
de administración de c<strong>la</strong>ves; puede asegurar <strong>la</strong> contraseña con cifrado, <strong>en</strong> cuyo caso<br />
ti<strong>en</strong>e que p<strong>en</strong>sar <strong>en</strong> cómo almac<strong>en</strong>ar <strong>la</strong> c<strong>la</strong>ve de cifrado. Si <strong>la</strong> c<strong>la</strong>ve se convierte <strong>en</strong><br />
vulnerable, un atacante puede descifrar todas <strong>la</strong>s contraseñas <strong>del</strong> almacén de <strong>datos</strong>.<br />
La opción preferida es:<br />
Almac<strong>en</strong>ar un valor hash unidireccional de <strong>la</strong> contraseña. Vuelva a<br />
calcu<strong>la</strong>r el valor hash cuando haya que validar <strong>la</strong> contraseña.<br />
Combine el valor hash de <strong>la</strong> contraseña con un valor salt (un número<br />
aleatorio de criptografía segura). Al combinar el valor salt con el valor hash<br />
de <strong>la</strong> contraseña, se reduce <strong>la</strong> am<strong>en</strong>aza asociada a los ataques de diccionario.<br />
Crear un valor salt<br />
El sigui<strong>en</strong>te código muestra cómo g<strong>en</strong>erar un valor salt con <strong>la</strong> funcionalidad de<br />
g<strong>en</strong>eración de números aleatorios que proporciona <strong>la</strong> c<strong>la</strong>se<br />
RNGCryptoServiceProvider <strong>en</strong> el espacio de nombres<br />
System.Security.Cryptography.<br />
public static string CreateSalt(int size)<br />
{<br />
}<br />
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();<br />
byte[] buff = new byte[size];<br />
rng.GetBytes(buff);<br />
return Convert.ToBase64String(buff);<br />
Crear un valor hash (con un valor salt)<br />
El sigui<strong>en</strong>te fragm<strong>en</strong>to de código muestra cómo g<strong>en</strong>erar un valor hash a partir de una<br />
contraseña suministrada y un valor salt.<br />
public static string CreatePasswordHash(string pwd, string salt)<br />
{<br />
}<br />
string saltAndPwd = string.Concat(pwd, salt);<br />
string hashedPwd =<br />
FormsAuth<strong>en</strong>tication.HashPasswordForStoringInConfigFile(<br />
return hashedPwd;<br />
saltAndPwd, "SHA1");