Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert string back to hashtable

Status
Not open for further replies.

iemerick

Programmer
Oct 7, 2005
6
US
i'm just starting my ventures into .NET

i'm trying to create a hash table to store in a cookie and then read it back as a string, convert it back to a hashtable and life is all good.

i dont know how to read it back though. Below is the 2 chunks of code i'm using to attempt this. The second chunch (the part that reads it) is the part that isn't working. I dont really expect it to in its current form, but in included it so you could see what I am trying to accomplish. Thanks

//create cookie with data
//-----------------------------------------------

string roles = GetRoles(myDataReader["username"].ToString());
Hashtable testHash = new Hashtable();
testHash.Add("roles", roles);
testHash.Add("uid", myDataReader["userID"]);
string stringedHash = testHash.ToString();

//store those roles in an encrypted ticked and then throw that ticket into the cookie
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, myDataReader["username"].ToString(), DateTime.Now, DateTime.Now.AddMinutes(60), false, stringedHash);
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
Response.Cookies.Add(authCookie);





//read cookie and convert back to hashtable
//----------------------------------------------------
string cookieData = authTicket.UserData.ToString();//Split(new char[]{','});
Hashtable testHash = (Hashtable)cookieData;

string roles = testHash["roles"].ToString();
string userID = testHash["uid"].ToString();
 
Since you're using the Forms Authentication system, you don't need to mess with any Hashtable. You'll want to chage a bit of code to this, though:

Code:
FormsAuthenticationTicket authTicket 
     = new FormsAuthenticationTicket(
            1, 
            myDataReader["username"].ToString(), 
            DateTime.Now, 
            DateTime.Now.AddMinutes(60), 
            false, 
            roles); //no need to use a Hashtable

You can access the user ID via:

Code:
Context.User.Identity.Name;

And you can see if the user is in a given role via:

Code:
Context.User.IsInRole("RoleName");
 
i am familiar with the Context.User.Identity.Name and roles objects.

I'm using a hash table because I want to store more than just these 2 pieces of information. With a hash table I can store any name/value pairs of my choosing, as opposed to just 1.

 
Then you don't want to use the FormsAuthenticationTicket. It's responsibility is to hold authentication information vs. additional configuration.

If you are using .NET 2.0, Profiles are suited to your task, and you may also use a separate cookie (which can store a set of sub-keys as name/value pairs).

Code:
//to set the cookie
HttpCookie infoCookie = new HttpCookie("KeyValuePairs");
infoCookie["Item1"] = "blah";
infoCookie["Item2"] = "whatever";
Response.Cookies.Add(infoCookie);

//to read the cookie
string item1 = Request.Cookies["KeyValuePairs"]["Item1"];
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top