2 Replies - 483 Views - Last Post: 19 February 2020 - 09:54 PM

#1 dongtrien   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 112
  • Joined: 17-June 15

Why can't I login to the web even though the User and password ?

Posted 19 February 2020 - 02:09 AM

Why can't I login to the web even though the User and password are correct ?

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
<appSettings>
   ...		
   <add key="SmtpServer" value="localhost"/>		
   <add key="EmailWebmaster" value="admin"/>		
   <add key="Password" value="123"/>
   ...
</appSettings>



In file Logon_Redirect.aspx
...
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>



In file Logon_Redirect.aspx.cs
...
public partial class Logon_Redirect : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // kiem tra va Redirect toi trang can thiet
            if (Page.User.IsInRole(Globals.Settings.AppRoles.KhachHang))
                Response.Redirect(Globals.ApplicationPath);
            else if (Page.User.IsInRole(Globals.Settings.AppRoles.Admin))
                Response.Redirect(Globals.ApplicationPath + "WebMaster/Contacts/Contact.aspx");
                        
        }
    }



In file Logon.aspx.cs
protected void btLogon_Click(object sender, EventArgs e)
  {
     //Login SQL Server very good
     if (Membership.ValidateUser(txtEmail.Text, txtPassword.Text)) 
     {        
        if (Request.QueryString["ReturnUrl"] != null)
        {
           FormsAuthentication.RedirectFromLoginPage(txtEmail.Text, false);
        }
        else
        {
           FormsAuthentication.SetAuthCookie(txtEmail.Text, false);
           Session["username"] = txtEmail.Text.Trim();
           Response.Redirect(Globals.ApplicationPath + "Logon_Redirect.aspx");
           //I am debugging and running to the code here and opens the Logon_Redirect.aspx file but nothing
        }
     }
     else //I can't Login User/Pass in file Web.config, check User: admin and Pass: 123 
     {
        // check User/pass other on SQL Server 
        if (webapp4U.BOL.User.CheckUserName(txtEmail.Text) && txtPassword.Text == ConfigurationManager.AppSettings["Password"].ToString())
        {
           FormsAuthentication.SetAuthCookie(txtEmail.Text, false);
           Session["username"] = txtEmail.Text.Trim();
           Response.Redirect(Globals.ApplicationPath + "Logon_Redirect.aspx");
        }
        else
        lblMsg.Text = ResourceManager.GetString("Logon_False");
     }
         
  }


This post has been edited by modi123_1: 19 February 2020 - 08:23 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Why can't I login to the web even though the User and password ?

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15564
  • View blog
  • Posts: 62,309
  • Joined: 12-June 08

Re: Why can't I login to the web even though the User and password ?

Posted 19 February 2020 - 08:25 AM

Have you put a break point there and checked all the if conditions for casing, spaces, and the like? Take out parts of any complex IFs to see if all AND conditions are correct?
Was This Post Helpful? 0
  • +
  • -

#3 dongtrien   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 112
  • Joined: 17-June 15

Re: Why can't I login to the web even though the User and password ?

Posted 19 February 2020 - 09:54 PM

View Postmodi123_1, on 19 February 2020 - 08:25 AM, said:

Have you put a break point there and checked all the if conditions for casing, spaces, and the like? Take out parts of any complex IFs to see if all AND conditions are correct?


I have set a break point but I do not know how to fix it, now I take the above example. Assume me the following user/pass:
I. in the Web.config file of the system administrator under "Administrator"
user: webAdmin
pass: 123
II. in SQL Server
- Case of "Administrator":
user: admin
pass: 123
- Case two of "Customers"
user: peter
pass: 123
My Question:
1. In the file Logon.aspx.cs I want to pass to Logon_Redirect.aspx.cs how do I declare ? Here, I want to add a message for the message Logon_Redirect.aspx.cs how to make the label I write ?
 
protected void btnLogin_Click(object sender, EventArgs e)
{
//Check User/Pass from SQL Server
if (Membership.ValidateUser(txtUsername.Text, txtPassword.Text))
{
if (Request.QueryString["ReturnUrl"] != null)
{
FormsAuthentication.RedirectFromLoginPage(txtUsername.Text, false);
}
else
{
...
Debug.Print("Login successfully to SQL Server ...");
//Here, I want to add a message for the message Logon_Redirect.aspx.cs how to make the label I write ?  
}
}
else //Check User/Pass from file Web.config
{
if (txtUsername.Text == ConfigurationManager.AppSettings["EmailWebmaster"].ToString() && txtPassword.Text == ConfigurationManager.AppSettings["Password"].ToString())
{
...
Debug.Print("Login successfully to Web.config ...");
//Here, I want to add a message for the message Logon_Redirect.aspx.cs how to make the label I write ? 
}
else
lblMsg.Text = ResourceManager.GetString("Logon_False");
}
}


2. I debug the Logon_Redirect.aspx.cs file in the following code I see:
 
1. protected void Page_Load(object sender, EventArgs e)
2. {
3. if (Page.User.IsInRole(Globals.Settings.AppRoles.KhachHang))
4. Response.Redirect(Globals.ApplicationPath);
5. else if (Page.User.IsInRole(Globals.Settings.AppRoles.Admin))
6. Response.Redirect(Globals.ApplicationPath + "WebMaster/Contacts/Contact.aspx");
7. }



2. I debug the Logon_Redirect.aspx.cs file in the following code I see:
- If I log in user: webAdmin, run command 1 to 5 and remove statement 6 and run statement 7
- If I log in user: admin, run the command from 1 to 7
- If I log in user: peter then run command 1 to 4 to remove statement 5, statement 6 and continue running command 7
- Summary: I login user: web failed, the remaining two cases login successfully (admin and peter).

This post has been edited by dongtrien: 19 February 2020 - 09:54 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1