开发者

Cookie doesn't persist

I've got an issue with my cookies. I'm authenticating users through LDAP and as long as the browser remains open users don't have to log back into the tool. They can even close the tab that's fine so long as the browser is open.

But the cookie gets removed the moment the user closes the browser. I've searched google a lot for this and I can't get any of the solutions to work such as this one or that one for example.

Here's my setup once they authenticate on my logon page:

String encryptedTicket = FormsAuthentication.Encrypt(authTicket);

//Create a cookie, and then add the encrypted ticket to the cookie as data.
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

//Add expiration date to the cookie
authCookie.Expires = DateTime.Now.AddMonths(1);

//Add the cookie to the outgoing cookies collection.
Response.Cookies.Add(authCookie);

//You can redirect now.
FormsAuthentication.RedirectFromLoginPage(txtUsername.Text, false);

My Web.Config looks like this:

<authentication mode="Forms"> <forms loginUrl="Logon.aspx" timeout="43200" name="adAuthCookie" path="/" slidingExpiration="true" /> </authentication>

Whatever I do the ASP.NET_SessionId and adAuthCookie cookies are always set to expire "When I close my browser". I want to avoid my users to always have to login when they clos开发者_运维百科e their browser and instead just do it once a month.


Sounds like you need to set a machineKey in your web.config. If one is not specified either there or machine.config, it will be generated on start up and reset everytime the website is restarted, invalidating the encryption in the cookie.

machineKey element


Make sure browser is not set to delete cookies on close. There is such paranoid setting somewher in security options (I think in IE it is Advanced -> Security ->Empty temporary folders...)

Use Fiddler or other tool to make sure you send cookies with correct expiration to the browser. This way you verify where error is server or client side.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜