c# – 为什么我的表单认证票证过期如此之快?

c# – 为什么我的表单认证票证过期如此之快?,第1张

概述我在ASP.NET应用程序中使用表单验证.我将FormsAuthenticationTicket配置为1年后到期,但实际在1小时左右后过期.我不知道为什么. 以下是登录过程中涉及的所有代码: public static bool Login(int id){ try { string securityToken = UserHelper.AuthenticateU 我在ASP.NET应用程序中使用表单验证.我将FormsAuthenticationTicket配置为1年后到期,但实际在1小时左右后过期.我不知道为什么.

以下是登录过程中涉及的所有代码:

public static bool Login(int ID){    try    {        string securityToken = UserHelper.AuthenticateUser(ID);        DateTime expiryDate = DateTime.Now.AddYears(1);        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(             1,ID.ToString(),DateTime.Now,expiryDate,true,securityToken,FormsAuthentication.FormscookiePath);        string encryptedTicket = FormsAuthentication.Encrypt(ticket);        @R_301_6822@cookie cookie = new @R_301_6822@cookie(FormsAuthentication.Formscookiename,encryptedTicket);        cookie.Expires = expiryDate;        @[email protected](cookie);        return true;    }    catch    {        return false;    }}

Web.config文件:

<system.web>    <machineKey valIDationKey="autoGenerate"    decryptionKey="autoGenerate" valIDation="SHA1" />    <compilation deBUG="true">    <authentication mode="Forms">        <forms loginUrl="~/Login.aspx" timeout="2880"/>    </authentication>...

我的做法有问题吗?为什么它过期如此之快?

编辑

全球代码:

protected voID Application_AuthenticateRequest(object sender,EventArgs e){    if (Request.PhysicalPath.EndsWith(".aspx") || Request.PhysicalPath.EndsWith(".axd") || Request.PhysicalPath.EndsWith(".ashx"))        SecurityManager.SetPrincipal();}

SetPrincipal代码:

public static voID SetPrincipal(){    IlivrePrincipal principal = null;    FormsIDentity IDentity;    UrlParameters urlParameters = UrlParametersHelper.GetUrlParameters(@[email protected]);    if (@[email protected])    {        IDentity = (FormsIDentity)@[email protected];        User userProfile;        urlParameters.SecurityToken = (((FormsIDentity)IDentity).Ticket).UserData;        try        {            userProfile = UserHelper.GetUser(urlParameters.SecurityToken);            UserHelper.UpdateLastActiveOn(userProfile);            principal = new AuthenticatedPrincipal(IDentity,userProfile);        }        catch        {            //Todo: Log an exception            FormsAuthentication.SignOut();            principal = new AnonymousPrincipal(new GuestIDentity(),UserHelper.GetUser(null));        }    }    else    {        principal = new AnonymousPrincipal(new GuestIDentity(),UserHelper.GetUser(null));    }    @[email protected] = principal;}
解决方法 这是你的问题.
<machineKey valIDationKey="autoGenerate"             decryptionKey="autoGenerate"             valIDation="SHA1"/>

每当应用程序池循环使用时,ASP将会生成一个新的机器密钥.哪个时间可以合理地发生.

机器密钥用于加密和解密您的FormsAuthentication cookie.如果它更改,浏览器上的cookie不再是任何好的.所以系统会对待你,好像你从来没有登录过.

尝试生成静态密钥并将其添加到配置文件中.应该看起来像这样:

<machineKey      valIDationKey="21F090935F6E49C2C797F69(snip)F1B72A7F0A281B"              decryptionKey="ABAA84D7EC4BB56D75D(snip)B8BF91CFCD64568A145BE59719F"    valIDation="SHA1"    decryption="AES"/>

生成一个关键here.

总结

以上是内存溢出为你收集整理的c# – 为什么我的表单认证票证过期如此之快?全部内容,希望文章能够帮你解决c# – 为什么我的表单认证票证过期如此之快?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1261159.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存