First of all, thanks a million for all the help. I really do not know how to write this in Google, so here it goes. I'm currently concerned about a method I'm implementing in my web app. I'm using ASP.NET MVC 5 with EF6 and Identity 2.
There are many times in which I need to pass a specific row ID from a View to a Controller. The best approach I've come up with is to put it into a hidden form encrypted.
Here is the code I'm using:
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;
}
}
I would later on get them back in the controller and check for any integrity problems.
I was suggested to avoid using hidden html forms because they could be easily cracked.
Unfortunately, they are also suggesting me to use Session[] which bumps into the following article: http://brockallen.com/2012/04/07/think-twice-about-using-session-state/
Which says that I should avoid the use of Session[] at all.
Also, this comment makes lots of sense:
Note that Session could introduce bugs when someone attempts to edit multiple forms at once in different tabs. When they save one, the Session value taken would be from the last one they loaded up to save... likely not what you will want, and it would likely be difficult to figure this out.
From here: https://stackoverflow.com/a/4224371/1057052
What is the recommended way to this?
Thanks a million!!