Saturday, March 24, 2012

Simple Cookie Question

Sorry another probably easy question...

I have a login procedure where I set a cookie - Below

FormsAuthentication.SetAuthCookie(LoggedInUserId, True)

Can I append other information within this cookie? not just the UserId and the Boolean? If I can whats the syntax?

Thanks in advance...

I am afraid you cant. Remember you are not the one who is writing this cookie, you are only calling this method, which inturn call another method in the framework to write the cookie, based on the parameters you provide here. If you want to find out how it works, use Reflector tool to look at the call stack

Ok. I take it back. I think there is actually a way. Look at the following code from the Reflector

publicstaticvoidSetAuthCookie(string userName,bool createPersistentCookie){FormsAuthentication.Initialize();FormsAuthentication.SetAuthCookie(userName,createPersistentCookie,FormsAuthentication.FormsCookiePath);}

publicstaticvoidSetAuthCookie(string userName,bool createPersistentCookie,string strCookiePath){FormsAuthentication.Initialize();HttpContext.Current.Response.Cookies.Add(FormsAuthentication.GetAuthCookie(userName,createPersistentCookie,strCookiePath));}

So, that is what is happening inside the SetAuthCookie method, so you can actually bypass this method and write your own version
You could do it by overriding the SetSuthCookie method. Is there a reason you cannot just add another cookie with the information you need?

Thanks for the answers lads... Appreciated...

I think I need to look into how to set my own cookie... Got any reference sites or examples about setting your own cookies??

Thanks in advance


http://samples.gotdotnet.com/quickstart/aspplus/doc/stateoverview.aspx#cookies

This is a whole page on managing state in ASP.NET, of which using cookies is one technique.


Thanks Lee...

0 comments:

Post a Comment