Jan 29, 2018 01:53 AM|JORGEMAL|LINK
Users of one page in my website need to be uniquely identified without the need of creating an account with username and password. To solve this issue I decided that generating a new GUID was a good approach. Also, I thought that saving the GUID in a cookie
is a good idea in order to avoid new GUID´s for the same user every time he/she visits the web page although I am aware that if cookies are disabled then they won´t be saved. Below is the code I wrote to achieve my goal. As this is the first time I use an
approach like this, I would like you to let me know if my code is fine and how it can be improved. Also, is GUID a safe way to generate unique id´s?
protected String GetCookies()
String strUserId = "";
// If cookie exists then I get the user id.
// If cookie does not exist then a new one is created with 1-year expiration time.
if (Request.Cookies["ccIduser"] != null)
HttpCookie UserIdCookie = Request.Cookies["ccIduser"];
strUserId = UserIdCookie.Value.ToString();
HttpCookie UserCookie = new HttpCookie("ccIduser");
strUserId = Guid.NewGuid().ToString();
UserCookie.Value = strUserId;
UserCookie.Expires = DateTime.Now.AddYears(1);