27 February 2014

Authentication Using XML

How to make users in XML File and authenticate them..
Solution:
create a xml file as

<mydata>
<tb>
<user>admin</user>
<pass>admin</pass>
</tb>
<tb>
<user>admin1</user>
<pass>admin1</pass>
<tb>
<tb>
<user>admin2</user>
<pass>admin2</pass>
<tb>
</mydata>


Add two Text Boxes on Design page with ids TextBox1 and TextBox2 
Add a label with id Label1
Add a button
double click on button
and write following code in button click event

//code to authenticate user
String pt = Server.MapPath("XMLFile.xml");//
DataSet ds = new DataSet();
ds.ReadXml(pt);
DataView dv = ds.Tables[0].DefaultView;
dv.RowFilter = "user='" + TextBox1.Text+"'";
if (dv.Count == 0)
Label1.Text = "Wrong User";
else
{
if (dv[0][1].ToString() == TextBox2.Text)
FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, false);
else
Label1.Text = "Wrong Password";
}

No comments:

Post a Comment