表单身份验证注销速度非常快,本地工作正常!

发布于 2024-08-30 04:16:06 字数 2669 浏览 5 评论 0原文

我的托管公司面临一个问题,我使用一个使用 FormsAuthentication 的项目,问题是虽然它成功登录,但它很快就注销了,我不知道可能是什么原因, 所以在我的 web.config 文件中我添加了这些行:

<authentication mode="Forms" >
  <forms name="Nadim" loginUrl="Login.aspx" defaultUrl="Default.aspx" protection="All" path="/" requireSSL="false"/>
</authentication>
<authorization>
  <deny users ="?" />
</authorization>


<sessionState mode="StateServer" stateConnectionString="tcpip=localhost:42424"  cookieless="false"  timeout="1440">
</sessionState>

这是我在自定义登录页面中使用的代码:

protected void PasswordCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
    {
        try
        {
            UsersSqlDataSource.SelectParameters.Clear();
            UsersSqlDataSource.SelectCommand = "Select * From Admins Where AdminID='" + IDTextBox.Text + "' and Password='" + PassTextBox.Text + "'";
            UsersSqlDataSource.SelectCommandType = SqlDataSourceCommandType.Text;

            UsersSqlDataSource.DataSourceMode = SqlDataSourceMode.DataReader;

            reader = (SqlDataReader)UsersSqlDataSource.Select(DataSourceSelectArguments.Empty);
            if (reader.HasRows)
            {
                reader.Read();
                if (RememberCheckBox.Checked == true)
                    Page.Response.Cookies["Admin"].Expires = DateTime.Now.AddDays(5);
                args.IsValid = true;

                string userData = "ApplicationSpecific data for this user.";

                FormsAuthenticationTicket ticket1 = new FormsAuthenticationTicket(1, IDTextBox.Text, System.DateTime.Now, System.DateTime.Now.AddMinutes(30), true, userData, FormsAuthentication.FormsCookiePath);
                string encTicket = FormsAuthentication.Encrypt(ticket1);
                Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));
                Response.Redirect(FormsAuthentication.GetRedirectUrl(IDTextBox.Text, RememberCheckBox.Checked));

                //FormsAuthentication.RedirectFromLoginPage(IDTextBox.Text, RememberCheckBox.Checked);
            }
            else
                args.IsValid = false;

        }
        catch (SqlException ex)
        {
            ErrorLabel.Text = ex.Message;
        }
        catch (InvalidOperationException)
        {
            args.IsValid = false;
        }
        catch (Exception ex)
        {
            ErrorLabel.Text = ex.Message;
        }

此外,您还会发现该行代码: FormsAuthentication.RedirectFromLoginPage(IDTextBox.Text, RememberCheckBox.Checked); 被评论是因为我认为登录时票证可能有问题,所以我手动创建了它,我知道的每件事我都尝试过,但没有任何效果, 那么有人知道问题是什么吗? 提前致谢, 巴赫。

There's a problem that i am facing with my hosting company, I use a project that uses FormsAuthentication and the problem is that though it successfully logs in, it logs out VERY QUICKLY, and i don't know what could be the cause of that,
so in my web.config file i added those lines:

<authentication mode="Forms" >
  <forms name="Nadim" loginUrl="Login.aspx" defaultUrl="Default.aspx" protection="All" path="/" requireSSL="false"/>
</authentication>
<authorization>
  <deny users ="?" />
</authorization>


<sessionState mode="StateServer" stateConnectionString="tcpip=localhost:42424"  cookieless="false"  timeout="1440">
</sessionState>

and this is the code i use in my custom login page :

protected void PasswordCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
    {
        try
        {
            UsersSqlDataSource.SelectParameters.Clear();
            UsersSqlDataSource.SelectCommand = "Select * From Admins Where AdminID='" + IDTextBox.Text + "' and Password='" + PassTextBox.Text + "'";
            UsersSqlDataSource.SelectCommandType = SqlDataSourceCommandType.Text;

            UsersSqlDataSource.DataSourceMode = SqlDataSourceMode.DataReader;

            reader = (SqlDataReader)UsersSqlDataSource.Select(DataSourceSelectArguments.Empty);
            if (reader.HasRows)
            {
                reader.Read();
                if (RememberCheckBox.Checked == true)
                    Page.Response.Cookies["Admin"].Expires = DateTime.Now.AddDays(5);
                args.IsValid = true;

                string userData = "ApplicationSpecific data for this user.";

                FormsAuthenticationTicket ticket1 = new FormsAuthenticationTicket(1, IDTextBox.Text, System.DateTime.Now, System.DateTime.Now.AddMinutes(30), true, userData, FormsAuthentication.FormsCookiePath);
                string encTicket = FormsAuthentication.Encrypt(ticket1);
                Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));
                Response.Redirect(FormsAuthentication.GetRedirectUrl(IDTextBox.Text, RememberCheckBox.Checked));

                //FormsAuthentication.RedirectFromLoginPage(IDTextBox.Text, RememberCheckBox.Checked);
            }
            else
                args.IsValid = false;

        }
        catch (SqlException ex)
        {
            ErrorLabel.Text = ex.Message;
        }
        catch (InvalidOperationException)
        {
            args.IsValid = false;
        }
        catch (Exception ex)
        {
            ErrorLabel.Text = ex.Message;
        }

Also you will find that line of code:
FormsAuthentication.RedirectFromLoginPage(IDTextBox.Text, RememberCheckBox.Checked);
is commented because i thought there might be something wrong with the ticket when i log in , so i created it manually , every thing i know i tried but nothing worked,
so does anyone have any idea what is the problem ?
Thanks in advance,
Baher.

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

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

发布评论

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

评论(2

如梦初醒的夏天 2024-09-06 04:16:06

您正在修改不需要修改的内容。

删除 Response.Redirect(FormsAuthentication 行,取消注释重定向并尝试将列出的配置替换为

<authentication mode="Forms" />
<authorization>
  <deny users ="?" />
</authorization>
<sessionState mode="StateServer" stateConnectionString="tcpip=localhost:42424"  cookieless="false"  timeout="1440"/>

You are modifying things that don't need to be modified.

Remove the Response.Redirect(FormsAuthentication line, uncomment the redirect and try replacing your listed config with

<authentication mode="Forms" />
<authorization>
  <deny users ="?" />
</authorization>
<sessionState mode="StateServer" stateConnectionString="tcpip=localhost:42424"  cookieless="false"  timeout="1440"/>
深海蓝天 2024-09-06 04:16:06

我认为您的问题可能出在 stateConnectionString="tcpip=localhost:42424" 上,也许您需要为您的提供商设置不同的 URL。

I think your problem maybe in the stateConnectionString="tcpip=localhost:42424" maybe you need to setup a different URL for your provider.

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