Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I trying to check login credential from XML file

What I have tried:

I tried following code,
Any suggestions as i used .where
it shows it cannpot converted to bool.

C#
<pre>protected void Submit_Click(object sender, EventArgs e)
        {
            string Username = txtUsername.Text;
            string Password = EncDec.Encrypt(txtPassword.Text);
            string FilePath = Server.MapPath("~/Logs/Login.xml");
            XDocument Loginxml = new XDocument();
            var LoginCheck = Loginxml.Descendants("userDetails")
                .Where(x => (string)x.Attribute("Username") == Username && (string)x.Attribute("Password") == Password );
            if (LoginCheck)
            {
                DisplayMessage.Visible = true;
                DisplayMessage.Text = "Login Successful";
                Response.Redirect("AdminMaster.aspx");
            }
            else
            {
                DisplayMessage.Visible = true;
                DisplayMessage.Text = "Invalid Username/Password";
            }
        }



XML Content:-
XML
<userDetails Username="TestAdmin"  EmailID="shridhar.salunkhe@grassdew.com" Password="YhY3rY4RCt9XVUiNfaOKmCMP7HADqexWdFHfXc4k3hQ=" />
Posted
Updated 7-Mar-17 23:55pm
Comments
Richard Deeming 9-Mar-17 12:47pm    
That looks like a security breach waiting to happen!

Unless you take specific steps to secure it, anyone can download the Logs/Login.xml file and read all of the credentials for your site.

You're storing the passwords in plain-text, so they don't even need to expend any effort to use the credentials - just copy and paste, and you're logged in.

Also, since you're not using any access control, there's nothing to stop a user navigating directly to AdminMaster.aspx without having to log in.

Don't try to re-invent the wheel. Use one of the built-in security systems instead - for example, ASP.NET Identity[^].

The code look a little bit odd

1. The XML document wasn't loaded

This line
C#
XDocument Loginxml = XDocument.Load();

Should be
C#
XDocument Loginxml = XDocument.Load(FilePath);

2. The LoginCheck is a type IEnumerable, not a boolean, maybe the code should check if the query return any result, I'm assuming each username and password is unique

C#
if (LoginCheck.Count() == 1)
            {

            }
            else
            {

            }
 
Share this answer
 
Here is the 100% full working code of your problem

string Username = "TestAdmin";
       string Password = "YhY3rY4RCt9XVUiNfaOKmCMP7HADqexWdFHfXc4k3hQ=";
       string FilePath = Server.MapPath("~/Login.xml");
       XDocument Loginxml = XDocument.Load(FilePath);
       var LoginCheck = Loginxml.Descendants("userDetails")
                .Where(x => (string)x.Attribute("Username") == Username &&      (string)x.Attribute("Password") == Password);
       if (LoginCheck.Any())
       {

           Response.Redirect("AdminMaster.aspx");
       }
       else
       {
           Response.Redirect("Login.aspx");
       }


Step 1: you missed to load the file

XDocument Loginxml = XDocument.Load(FilePath);


Step 2: it shows it cannot converted to bool because it return enumerable type but you are declared it to use like bool, better is to use the way i have pasted suggested solution.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900