Dong Lam Trien

Dong Lam Trien

  • 775
  • 968
  • 134.1k

Why can’t I login to the web even though the User and pass ?

Jan 10 2020 2:51 AM
I am writing a small example of Login webSite, there are two types of accounts and passwords, one is an account and password is stored in the Web.config file and the other two accounts and passwords are saved in SQL Server database, My problem is that in form 1, when logging in it opens Logon_Redirect.aspx file but cannot access, the following is my code
 
I am debugging and running to the code where this opens the Logon_Redirect.aspx file but nothing, but when I log in with another account and password (the user password of SQL Server) log in well.
 
In file Web.config
  1. <appSettings>  
  2. ...  
  3. <add key="SmtpServer" value="localhost"/>  
  4. <add key="EmailWebmaster" value="admin"/>  
  5. <add key="Password" value="123"/>  
  6. ...  
  7. </appSettings>   
In file Logon_Redirect.aspx
  1. <html xmlns="http://www.w3.org/1999/xhtml" >  
  2. <head runat="server">  
  3. <title>Untitled Page</title>  
  4. </head>  
  5. <body>  
  6. <form id="form1" runat="server">  
  7. <div>  
  8. </div>  
  9. </form>  
  10. </body>  
  11. </html> 
In file Logon_Redirect.aspx.cs
  1. public partial class Logon_Redirect : System.Web.UI.Page  
  2. {  
  3.      protected void Page_Load(object sender, EventArgs e)  
  4.     {  
  5.           // kiem tra va Redirect toi trang can thiet  
  6.           if (Page.User.IsInRole(Globals.Settings.AppRoles.KhachHang))  
  7.           Response.Redirect(Globals.ApplicationPath);  
  8.           else if (Page.User.IsInRole(Globals.Settings.AppRoles.Admin))  
  9.           Response.Redirect(Globals.ApplicationPath + "WebMaster/Contacts/Contact.aspx");  
  10.      }  
  11. }  
In file Logon.aspx.cs
  1. protected void btLogon_Click(object sender, EventArgs e)  
  2. {  
  3.      //I can't Login User/Pass in file Web.config, check User: admin and Pass: 123  
  4.      if (Membership.ValidateUser(txtEmail.Text, txtPassword.Text))  
  5.      {              
  6.             if (Request.QueryString["ReturnUrl"] != null)  
  7.      {  
  8.       FormsAuthentication.RedirectFromLoginPage(txtEmail.Text, false);  
  9.      }  
  10.      else  
  11.     {  
  12.           FormsAuthentication.SetAuthCookie(txtEmail.Text, false);  
  13.           Session["username"] = txtEmail.Text.Trim();  
  14.           Response.Redirect(Globals.ApplicationPath + "Logon_Redirect.aspx");  
  15.           //I am debugging and running to the code here and opens the Logon_Redirect.aspx file but nothing   
  16.     }  
  17.     }  
  18.    else  //Login SQL Server very good  
  19.    {  
  20.           // check User/pass other on SQL Server  
  21.           if (webapp4U.BOL.User.CheckUserName(txtEmail.Text) && txtPassword.Text ==             ConfigurationManager.AppSettings["Password"].ToString())  
  22.           {  
  23.                  FormsAuthentication.SetAuthCookie(txtEmail.Text, false);  
  24.                  Session["username"] = txtEmail.Text.Trim();  
  25.                  Response.Redirect(Globals.ApplicationPath + "Logon_Redirect.aspx");  
  26.          }  
  27.         else  
  28.                 lblMsg.Text = ResourceManager.GetString("Logon_False");  
  29.      }  
  30. }  

Answers (8)