Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i wanna to know how can i encrypting and decrypting cookies asp.net
i use cookie utility where i create delete and read my cookie
this is my code

C#
public static void CreateCookie(string CookieName, string[] keys, string[] values, bool Expired, HttpResponse res)
{
    HttpCookie c = new HttpCookie(CookieName);
    if (keys != null)
    {
        for (int x = 0; x < keys.Length; x++)
            c.Values.Add(keys[x], values[x]);
        if (!Expired)

            c.Expires = DateTime.Now.AddDays(2);
    }
    else

        c.Expires = DateTime.Now.AddDays(-2);
    res.Cookies.Add(c);
}



public static string ReadFromCookie(string CookieName, string key, HttpRequest req)
{
    try
    {
        return req.Cookies[CookieName][key].ToString();
    }
    catch
    {

        return null;
    }

}



public static void RemoveCookie(string CookieName, HttpResponse res)
{
    CreateCookie(CookieName, null, null, false, res);
}


in my login page i create cookie by this code


C#
protected void btnLogin_Click(object sender, EventArgs e)
   {

       Member m = new Member();
       if (m.Login(Txtuser.Text, Txtpass.Text))
      {

          utility.CreateCookie("login", new string[] { "user", "pass" }, new string[] { Txtuser.Text, Txtpass.Text }, ChkReM.Checked, Response);
          Redirect(Txtuser.Text);
      }
      else
          lblmsg.Text = "Username/password incorrect";
      }



   private void Redirect(string user)
   {


       if (user == "Admin")
           Response.Redirect("Admin/Admin.aspx");

       else
       {
           Response.Redirect("Users/UserInfo.aspx");
       }
   }


how can i encrypting and decrypting my cookies in this code
Posted
Comments
Dholakiya Ankit 20-Jul-13 8:29am    
which encryption technique you want to use?
ost3z 20-Jul-13 14:30pm    
any suitable one with cookie

1 solution

Google is your friend: Be nice and visit him often. He can answer questions a lot more quickly than posting them here...

A very quick search using your subject as the search term gave 300,000 hits: Google[^]

In future, please try to do at least basic research yourself, and not waste your time or ours.
 
Share this answer
 
Comments
ost3z 20-Jul-13 13:44pm    
first you must know that when i ask a question i searched before and do my best and so when when ask it i really want answer and didint want to waste my time with you and if your time so important why did you waste time to post here ???
OriginalGriff 20-Jul-13 15:31pm    
Because it didn't waste much of my time; less than a minute to read your question, Google, find the answer among the results, and reply to you.
So if I can do it in less than a minute, it doesn't look like you searched too hard, does it?
ost3z 20-Jul-13 16:43pm    
really i find many answers in Google and examples but i need to now how to use it with cookie utility because i try them but no sense
ost3z 20-Jul-13 21:10pm    
i used this links and i will try again thanks for all
http://rollercoasters-bunker.blogspot.in/2008/07/secureencrypted-cookies-in-aspnet-with.html

http://www.c-sharpcorner.com/UploadFile/manishkdwivedi/encrypting-and-decrypting-cookies-in-Asp-Net-2-0/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900