gpt4 book ai didi

c# - Persistent AuthCookie 已设置但被重定向到登录

转载 作者:太空狗 更新时间:2023-10-30 01:03:17 24 4
gpt4 key购买 nike

我在使用持久性 AuthCookie 时遇到问题。验证和登录工作完美,如果我关闭浏览器并重新打开它,身份验证仍然有效,没有重定向到登录页面。我不确定确切的时间是什么,但假设如果在不注销的情况下关闭浏览器并在 20 分钟后才重新打开它,即使在我使用 Web 开发人员工具检查时设置了 cookie,我也会被重定向到登录页面它的到期日期是一个月后。

验证用户凭据后我所做的就是

FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);

在我的 Web.Config 中,我将其设置为

<configuration>
<system.web>
...
<authentication mode="Forms">
<forms cookieless="UseCookies" loginUrl="~/Utilizador/Login" name="SMOAuth" slidingExpiration="true" timeout="43829"/>
</authentication>
...

还尝试按照建议对机器 key 进行硬编码here和其他一些地方,但没有效果

<machineKey validationKey="Validation_Key_Here" decryptionKey="Decrypt_Key_Here" validation="SHA1" decryption="AES"/>

我很难弄明白这一点

最佳答案

//this line is NOT ENOUGH for "remember me" to work!!!
FormsAuthentication.SetAuthCookie(userName, true); //DOESN'T WORK!

//###########

//you have to save the "remember me" info inside the auth-ticket as well
//like this:

DateTime expires = DateTime.Now.AddDays(20); //remember for 20 days

//create the auth ticket
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
userName,
DateTime.Now,
expires, // value of time out property
true, // Value of IsPersistent property!!!
String.Empty,
FormsAuthentication.FormsCookiePath);

//now encrypt the auth-ticket
string encryptedTicket = FormsAuthentication.Encrypt(ticket);

//now save the ticket to a cookie
HttpCookie authCookie = new HttpCookie(
FormsAuthentication.FormsCookieName,
encryptedTicket);
authCookie.Expires = expires;

//feed the cookie to the browser
HttpContext.Current.Response.Cookies.Add(authCookie);

关于c# - Persistent AuthCookie 已设置但被重定向到登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29922761/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com