Results 1 to 10 of 10

Thread: Encrypting and Decrypting cookie data?

  1. #1

    Thread Starter
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464

    Encrypting and Decrypting cookie data?

    I need to store cookies on the users computer to allow the forums I have created to remember them.

    The question I have is, what is a good way to encrypt the information sent with the cookie, and decrypt it when it comes back to the web app? I don't want usernames and passwords traveling over the Interent un-encrypted. How do I encrypt and decrypt information based on a 'phrase' that I choose?


    I have been using the built in functionality of the .Net framework, but it seems that I can't get it to remember the user when they come back even though I am expiring the cookie 6 months from the last date the user visits. In case your wondering why I don't use it.

  2. #2
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    try maybe using applets?
    \m/\m/

  3. #3
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Hellswraith,
    Here is the code I use for my authentication. So far, I have not been able to detect any bugs with persisting the user data/roles:

    Code:
    protected void Application_AuthenticateRequest(Object sender, EventArgs e)
    {
        if (Context.Request.IsAuthenticated) {	
    	HttpContext context = HttpContext.Current;
    	string roles; string[] userRoles = null;
    	if ((context.Request.Cookies[rolesCookie] == null) || (context.Request.Cookies[rolesCookie].Value == String.Empty)) {
    	    roles = String.Join(";", VegaSoft.VSForums.Business.User.GetRolesByUser(context.User.Identity.Name));
    	    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, context.User.Identity.Name, DateTime.Now, DateTime.Now.AddHours(1), false, roles);
    	    context.Response.Cookies[rolesCookie].Value = FormsAuthentication.Encrypt(ticket);
    	    context.Response.Cookies[rolesCookie].Expires = DateTime.Now.AddMinutes(5);	
    	    userRoles = roles.Split(new char[] {';'});
    	}
    	else {
    	    FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(Context.Request.Cookies[rolesCookie].Value);
    	    if (ticket.Name != Context.User.Identity.Name) {
    		Context.Response.Cookies[rolesCookie].Expires = DateTime.Now; return;						
    	    }
    	    else {
    		ArrayList rolesList = new ArrayList();
    		foreach (string role in ticket.UserData.Split(new char[] {';'})) {
    		    if (role.Length > 0) {
    		        rolesList.Add(role);
    		    }
    		}
    		userRoles = (string[]) rolesList.ToArray(typeof(string));						
    	    }
    	}
    	context.User = new GenericPrincipal(Context.User.Identity, userRoles);		 
        }		
    }
    Login:
    Code:
    private void RegisterUser_Click(object sender, System.EventArgs e) 
    {
        if (Page.IsValid) {
    	ErrorMsg.Text = String.Empty;
    	VSForums.Business.User usr = new VSForums.Business.User();
    	usr.UserName = UserName.Text.Trim();
    	usr.Password = Password.Text.Trim();
    	if (VSForums.Business.User.ValidateLogin(usr) == Enums.User.LoginUserStatus.InvalidLogin) {
    	    ErrorMsg.Text = InvalidCredentialsMsg;
    	}
    	else {
    	    FormsAuthentication.SetAuthCookie(UserName.Text, KeepLoggedIn.Checked);
    	    string redirectUrl = Page.Request.QueryString["ReturnUrl"];
    	    if (redirectUrl != null) {
    		Page.Response.Redirect(redirectUrl, true);
    	    }
    	    else {
    		Response.Redirect("/VSForums/index.aspx", true);		
    	    }						
    	}
        }
    }
    Last edited by Lethal; May 22nd, 2003 at 11:29 PM.

  4. #4

    Thread Starter
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Thanks, I will give it a try later.

  5. #5
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    hmmm i think you cant use the code, there is something missing:

    VegaSoft.VSForums.Business.User.GetRolesByUser(context.User.Identity.Name)); in the first piece of code! be aware
    \m/\m/

  6. #6
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Originally posted by PT Exorcist
    hmmm i think you cant use the code, there is something missing:

    VegaSoft.VSForums.Business.User.GetRolesByUser(context.User.Identity.Name)); in the first piece of code! be aware
    I'm not following you....
    [ VegaSoft.VSForums.Business.User ] is a custom class I created, not the user class in class library.

  7. #7

    Thread Starter
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Originally posted by PT Exorcist
    hmmm i think you cant use the code, there is something missing:

    VegaSoft.VSForums.Business.User.GetRolesByUser(context.User.Identity.Name)); in the first piece of code! be aware
    I understand I wouldn't be able to just 'drop' it into my code. I will be able to adapt it to my needs. Thanks for looking out though.

  8. #8
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Ah, I was thinking he was making a suggestion to me. It's was early, cut me some slack..

  9. #9

    Thread Starter
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    lol,

    Have you tested it in all situations? My main problem is persistance:

    User logs in, then closes window. Reopen IE, go to site, not logged in anymore.

  10. #10
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Yes, I have tested it pretty heavily. I think its pretty solid, but since I just said that, I'm sure you'll find something....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width