Menu

Wednesday, September 22, 2010

User Login With XML C#


The following post demonstrates how to use a Login control to provide a user interface to log on to a Web site with XML.






Create New Web

Goto File > New > Web Site |  Then Select ASP.NET web site | Language c#

Goto Solution Explorer
Right Click On App_Data Then Click Add New Item
create a XML file and name it "AllUser.xml"

My xml file like this :

<?xml version="1.0" encoding="utf-8" ?>
<Staff>
      <User>
          <username>aspxcode</username>
          <password>2010</password>
      </User>  
      <User>
          <username>windows</username>
          <password>xp</password>
      </User>
</Staff>

1-Open Default.aspx On Design View
    Now Go To Toolbox >  Login > Double Click On Login Control
              

                     OR

   Open Default.aspx On Source View
   Paste This Code In Body Section

<form id="form1" runat="server"><div align="center">
<br />
<asp:Login ID="Login1" runat="server" BackColor="#EFF3FB" BorderColor="#B5C7DE" BorderPadding="4" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#333333" Height="120px" Width="298px"><TextBoxStyle Font-Size="0.8em" />
<LoginButtonStyle BackColor="White" BorderColor="#507CD1" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284E98" /><InstructionTextStyle Font-Italic="True" ForeColor="Black" />
<TitleTextStyle BackColor="#507CD1" Font-Bold="True" Font-Size="0.9em" ForeColor="White" /></asp:Login>
<br /></div></form>

After Done First Step You Have Login Control Like Mine















Now Double Click On The Login Control 
                           OR
Select Login1 Then Goto Properties > Select Events > Double Click On Authenticate

















ok now in source code page
 Add Namespace

using System.Xml;

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
        
string username;
        
string pwd;
        
string CurrentUser = "";
        
string CurrentPwd = "";
        
bool LoginStatus = false;

        username = Login1.UserName;
        pwd = Login1.Password;

        
XmlDocument xd = new XmlDocument();
        xd.Load(Server.MapPath(
"~/App_Data/AllUser.xml"));

        
XmlNodeList xnl = xd.GetElementsByTagName("User");

        
foreach (XmlNode xn in xnl)
        {
            
XmlNodeList cxnl = xn.ChildNodes;
            
foreach (XmlNode cxn in cxnl)
            {
              
 if (cxn.Name == "username")
                {
                    
if (cxn.InnerText == username)
                    {
                        CurrentUser = username;
                    }
                }

              
 if (cxn.Name == "password")
                {
                  
 if (cxn.InnerText == pwd)
                    {
                        CurrentPwd = pwd;
                    }
                }
            }

            
if ((CurrentUser != "") & (CurrentPwd != ""))
            {
                LoginStatus = true;
            }
        }

        
if (LoginStatus == true)
        {
            Session[
"UserAuthentication"] = username;
            Session.Timeout = 1;

            Response.Redirect(
"welcome-page.aspx");
        }

        else
        {
            Session[
"UserAuthentication"] = "";
        }

    }


Save And Run It :)



Download Source Code

1 comment: