首先,感谢一百万人的帮助。我真的不知道如何在谷歌中写这个,所以就这样吧。我目前关注的是我在网络应用中实施的方法。我使用带有EF6和Identity 2的ASP.NET MVC 5。
我需要多次将特定的行ID从View传递给Controller。我提出的最好方法是将其加密为隐藏形式。
以下是我使用的代码:
public class StringEncrypt : IEncrypt
{
public Func<string> GetUserID;
public StringEncrypt()
{
GetUserID = () => HttpContext.Current.User.Identity.GetUserId();
}
private string Purpose = "The authentication token is";
public string Protect(string unprotectedText)
{
var unprotectedBytes = Encoding.UTF8.GetBytes(unprotectedText);
var protectedBytes = MachineKey.Protect(unprotectedBytes, Purpose + GetUserID);
var protectedText = Convert.ToBase64String(protectedBytes);
return protectedText;
}
public string Unprotect(string protectedText)
{
var protectedBytes = Convert.FromBase64String(protectedText);
var unprotectedBytes = MachineKey.Unprotect(protectedBytes, Purpose + GetUserID);
var unprotectedText = Encoding.UTF8.GetString(unprotectedBytes);
return unprotectedText;
}
}
我稍后会将它们放回控制器并检查是否存在任何完整性问题。
我被建议避免使用hidden html forms because they could be easily cracked.
不幸的是,他们还建议我使用会话[],这些内容会碰到以下文章: http://brockallen.com/2012/04/07/think-twice-about-using-session-state/
其中说我应该避免使用Session []。
此外,这条评论很有意义:
请注意,当有人尝试在不同标签中同时编辑多个表单时,会话可能会引入错误。当他们保存一个时,会话值将来自他们加载的最后一个保存...可能不是你想要的,并且可能很难弄明白。
从这里开始:https://stackoverflow.com/a/4224371/1057052
推荐的方法是什么?
万分感谢!!