我的托管公司面临一个问题,我使用了一个使用 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); 被评论是因为我认为登录时票证可能有问题,所以我手动创建了它,我知道我尝试过的每件事都没有奏效,所以有人知道问题出在哪里吗?在此先感谢,巴赫。