登录后重定向对我不起作用

本文关键字:不起作用 重定向 登录 | 更新日期: 2023-09-27 17:55:43

我正在制作一个页面,一旦登录通过身份验证,它应该重定向到登录页面。我使用了登录向导。单击导航栏中的任何链接(如果未登录)时,它会重定向到单独的注册页面。正在发生的事情是,一旦我登录,它就会直接将我带到注册页面而不是登录页面。我包括登录页面和web.config的代码。顺便说一句,我正在使用引导程序。

登录页面:

public partial class Login : System.Web.UI.Page
{
    string UserLogins = ConfigurationManager.ConnectionStrings["DB1"].ConnectionString.ToString();
    public static object ConfigurationManager { get; private set; }
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
        Boolean blnresult;
        blnresult = false;
        blnresult = Authentication(Login1.UserName, Login1.Password);
        if (blnresult == true)
        {
            e.Authenticated = true;
            Response.Redirect("LandingPage.aspx");
        }
        else
        {
            e.Authenticated = false;
        }
    }
    protected static Boolean Authentication(string usernames, string passwords)
    {
        string sqlcmd;
        sqlcmd = "SELECT UName, Pwd from  [SignupDB] where UName = '" + usernames + "' and Pwd = '" + passwords + "'";
        OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/DB1.mdb;Persist Security Info=True;Jet OLEDB:Database Password=mypassword");
        con.Close();
        con.Open();
        OleDbCommand cmd = new OleDbCommand(sqlcmd, con);
        OleDbDataReader reader;
        reader = cmd.ExecuteReader();
        if (reader.Read())
            return true;
        else
            return false;
    }
}

web.config:

<add name="SignupDB" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&quot;C:'Users'admin'Documents'Visual Studio 2010'WebSites'mysite'App_Data'DB1.mdb&quot;;Persist Security Info=True;Jet OLEDB:Database Password=mypassword" providerName="System.Data.OleDb"/>
</connectionStrings>
<system.web>
<authentication mode="Forms">
    <forms loginUrl="RegPage.aspx" name="login" protection="Validation" defaultUrl="LandingPage.aspx" cookieless="UseCookies" timeout="200" enableCrossAppRedirects="true"/>
</authentication>
<authorization>
    <deny users="?"/>
</authorization>

登录后重定向对我不起作用

我认为重定向不是你的问题。问题是您没有使用表单身份验证进行日志记录,因此当您通过表单身份验证重定向到任何受保护的页面时,它会将您重定向回登录页面。看看此方法,以初始化窗体身份验证。

private void Authenticate(string login)
{
  FormsAuthentication.Initialize();
  var ticket = new FormsAuthenticationTicket(1,
                                             login,
                                             DateTime.Now,
                                             DateTime.Now.AddHours(3), // time of user's session .. add more if you need
                                             false,
                                             null);
  var encryptedTicket = FormsAuthentication.Encrypt(ticket);
  if (!FormsAuthentication.CookiesSupported)
  {
      FormsAuthentication.SetAuthCookie(encryptedTicket, false);
  }
  else
  {
      HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
      authCookie.Expires = ticket.Expiration;
      System.Web.HttpContext.Current.Response.Cookies.Add(authCookie);
  }  
}

您可以在登录成功时调用它,例如:

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
    Boolean blnresult;
    blnresult = false;
    blnresult = Authentication(Login1.UserName, Login1.Password);
    if(blnresult)
    {
        e.Authenticated = true;
        // authenticate using Forms Authentication
        Authenticate(Login1.UserName);
        Response.Redirect("LandingPage.aspx");
    }
    else
    {
        e.Authenticated = false;
    }
}

我还对您的数据库访问进行了一些改进,以确保您正在关闭数据库连接:

protected static bool Authentication(string usernames, string passwords)
{
    string sqlcmd = "SELECT UName, Pwd from  [SignupDB] where UName = '" + usernames + "' and Pwd = '" + passwords + "'";
    using(OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/DB1.mdb;Persist Security Info=True;Jet OLEDB:Database Password=mypassword"))
    {
      con.Open();
      OleDbCommand cmd = new OleDbCommand(sqlcmd, con);
      using(OleDbDataReader reader = cmd.ExecuteReader())
      {
        if (reader.Read())
            return true;
        else
            return false;
      }
    }    
}