fra*_*lic 22 c# asp.net encryption cryptography
我想加密ASP.NET中的cookie.
我已经按照本文中的方法进行了操作,但它的缺点是在内部方法上使用了反射.这导致它在代码审查中被标记 - 由于内部实现可能会发生变化,因此无法面向未来.
是否有一种具有相同功能的方法,不需要对内部方法使用加密?
我正在使用.NET Framework 3.5 SP1(假设我无法更改框架版本)
mat*_*ser 34
你不需要再自己动手了.
.Net 4.5有MachineKey.Protect()和MachineKey.Unprotect().
System.Web.Security.MachineKey
.Net 4.0有MachineKey.Encode()和MachineKey.Decode().您应该将MachineKeyProtection设置为'All'.这些现在已经过时了,如果你有4.5,你应该使用较新的.
请注意,如果您尝试在控制台应用程序而不是ASP.Net中使用它们,则每次重启应用程序时都会生成新密钥.我只是快速检查了它,但在ILSpy中,如果缺少相应的app.setting,它看起来会生成自己的默认值.
我找不到nonASP.Net等价物.
jba*_*all 19
为什么不使用System.Security.Cryptography中的加密来加密和解密cookie名称和敏感时的值?您可以编写一些实用程序函数来轻松管理它.示例效用函数:
private static void SetEncryptedCookie(string name, string value)
{
var encryptName = SomeEncryptionMethod(name);
Response.Cookies[encryptName].Value = SomeEncryptionMethod(value);
//set other cookie properties here, expiry &c.
//Response.Cookies[encryptName].Expires = ...
}
private static string GetEncryptedCookie(string name)
{
//you'll want some checks/exception handling around this
return SomeDecryptionMethod(
Response.Cookies[SomeDecryptionMethod(name)].Value);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
38123 次 |
| 最近记录: |