表单验证不起作用

发布于 2024-08-15 06:46:50 字数 2138 浏览 2 评论 0原文

我的网站在我的开发盒上按预期工作。也就是说,formsauthentication 票证将在 30 天后过期。这是通过以下代码实现的

string roles = UserManager.getAuthenticationRoleString(txtUsername.Text);

HttpCookie formscookie = FormsAuthentication.GetAuthCookie(txtUsername.Text, true);

FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(formscookie.Value);

FormsAuthenticationTicket newticket = new FormsAuthenticationTicket(1, ticket.Name, DateTime.Now, DateTime.Now.AddDays(30), true, roles, ticket.CookiePath);

HttpCookie newCookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(newticket));
newCookie.Expires = DateTime.Now.AddDays(30);
Response.Cookies.Add(newCookie);

我使用 fiddler 检查过期设置是否正确,我得到了这个

.ASPXAUTH=84AB5430CF4B1C5F9B59C9285288B41F156FCAFA2A169EACE17A7778A392FA69F66770FD8A08FFD06064B00F0BD788FEEC4A5894B7089239D6288027170A642B3B7EB7DB4806F2EBBCF2A82EE20FD944A38D2FE253B9D3FD7EFA178307464AAB4BCB35181CD82F6697D5267DB3B62BAD; expires=Thu, 21-Jan-2010 18:33:20 GMT; path=/; HttpOnly

所以我预计它会在 30 天后过期...但它只使它大约 30 分钟。

关于我的环境/代码,我还有 3 个其他有趣的花絮

  1. 在生产框中有两个站点指向相同的代码,一个用于外部访问,一个用于内部访问

  2. 当我确实因为过早过期而获得登录页面时,.ASPAUTH cookie 仍然存在并发送到浏览器

  3. global.asax 中存在一些角色检查,如下所示

-

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
    {

        if (HttpContext.Current.User != null)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                if (HttpContext.Current.User.Identity is FormsIdentity)
                {
                    FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
                    FormsAuthenticationTicket ticket = id.Ticket;

                    // Get the stored user-data, in this case, our roles
                    string userData = ticket.UserData;
                    string[] roles = userData.Split('|');
                    HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(id, roles);
                }
            }
        }
    }

I have a site that works as expected on my development box. That is, the formsauthentication ticket expires after 30 days. This is achieved through the following code

string roles = UserManager.getAuthenticationRoleString(txtUsername.Text);

HttpCookie formscookie = FormsAuthentication.GetAuthCookie(txtUsername.Text, true);

FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(formscookie.Value);

FormsAuthenticationTicket newticket = new FormsAuthenticationTicket(1, ticket.Name, DateTime.Now, DateTime.Now.AddDays(30), true, roles, ticket.CookiePath);

HttpCookie newCookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(newticket));
newCookie.Expires = DateTime.Now.AddDays(30);
Response.Cookies.Add(newCookie);

I used fiddler to check that the expiration is set properly and I get this

.ASPXAUTH=84AB5430CF4B1C5F9B59C9285288B41F156FCAFA2A169EACE17A7778A392FA69F66770FD8A08FFD06064B00F0BD788FEEC4A5894B7089239D6288027170A642B3B7EB7DB4806F2EBBCF2A82EE20FD944A38D2FE253B9D3FD7EFA178307464AAB4BCB35181CD82F6697D5267DB3B62BAD; expires=Thu, 21-Jan-2010 18:33:20 GMT; path=/; HttpOnly

So I would expect it to expire in 30 days...But it only makes it about 30 minutes.

I have 3 other interesting tidbits about my environment / code

  1. On the production box there are two sites pointing at the same code one for external access and one for internal access

  2. When the I do get the login page because of premature expiration, the .ASPAUTH cookie is still there and sent to the browser

  3. There is some role checking in the global.asax that looks like this

-

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
    {

        if (HttpContext.Current.User != null)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                if (HttpContext.Current.User.Identity is FormsIdentity)
                {
                    FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
                    FormsAuthenticationTicket ticket = id.Ticket;

                    // Get the stored user-data, in this case, our roles
                    string userData = ticket.UserData;
                    string[] roles = userData.Split('|');
                    HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(id, roles);
                }
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

舟遥客 2024-08-22 06:46:51

您需要将机器密钥标签添加到 web.config 文件中。它正在重新生成,这会导致您过早超时。

这类似于以下问题:

弄清楚为什么 asp.net身份验证票即将过期

You'll need to add a machine key tag to web.config file. It's getting regenerated and that causes your premature timeout.

This is similar to the following question:

figuring out why asp.net authentication ticket is expiring

妄想挽回 2024-08-22 06:46:51

如果问题是生产设备上的用户被踢出并且必须通过 FormsAuthentication 重新登录,则问题可能与 IIS 相关,而不是 .NET。

我之前遇到过问题,应用程序中的所有超时设置都没有产生影响,并且用户启动得太早。

检查网站和应用程序池的 IIS 设置。两者都有与超时等相关的设置。

如果II6:

  • 在网站属性下->主目录选项卡 ->配置按钮->选项选项卡 -> 在您站点的应用程序池下有会话状态/长度信息
  • ->性能和运行状况选项卡 ->两者都有几个可能会回收池的设置(并且本质上强制重新登录)

要调试,您可以禁用池上的所有运行状况和性能检查,但是要非常小心,因为如果应用程序失控,这可能会限制您的服务器。

您还可以尝试将超时设置放入 web.config 中:

<system.web>
    <authentication mode="Forms">
          <forms timeout="XXXXX"/>
    </authentication>
</system.web>

无论如何,这只是来自类似问题的个人经验的一些想法。希望它可能有所帮助!

If the problem is that the user on the production box is kicked and has to log in again via FormsAuthentication, it's possible that the problem is IIS related and not .NET.

I've run into issues before where all the timeout settings in the world within the app didn't make a difference and the user was booted far too early.

Check your IIS settings for both the website and the application pool. There are settings in both related to timeouts, etc.

If II6:

  • Under website properties -> Home Directory tab -> Configuration Button -> Options Tab -> there is session state/length info here
  • Under application pool for your site -> Performance and Health tabs -> both have several settings that may recycle your pool (and essentially force a re-logon)

To debug, you could disable all health and performance checks on the pool, however be very careful as this could throttle your server if the app gets out of control.

You could also try putting the timeout settings in the web.config:

<system.web>
    <authentication mode="Forms">
          <forms timeout="XXXXX"/>
    </authentication>
</system.web>

Anyway just some ideas from personal experience of similar issues. Hope it might help!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文