Login using XML file in ASP.NET using C#

Login using XML file in ASP.NET using C#


Posted in : ASP.Net Posted on : December 16, 2010 at 6:18 PM Comments : [ 0 ]

In this article we will introduce with Login using XML file in ASP.NET using C#.

Login using XML file in ASP.NET using C#

In this Example we will introduce how can we use login control and provide data using XML file. First you will make the XML file which is following:

Loginxml.xml

<?xml version="1.0" encoding="utf-8" ?>
<employee>
<User>
<username>bikrantsingh</username>
<password>bikrant123</password>
</User>
<User>
<username>anuragsingh</username>
<password>anurag</password>
</User>
<User>
<username>saurabh</username>
<password>saurabh123</password>
</User>
</employee>

LoginXML.aspx (Design Page):

LoginXML.aspx (source code):

<%@ Page Title="" Language="C#" MasterPageFile="~/RoseindiaMaster.master" AutoEventWireup="true"
CodeFile="LoginXML.aspx.cs" Inherits="LoginXML" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<div>
<h2 style="color: Green">
Login using Xml file in ASP.NET 4, C#</h2>
<asp:Login ID="Login1" runat="server" BackColor="#F7F7DE" BorderColor="#CCCC99" BorderStyle="Solid"
BorderWidth="1px" Font-Names="Verdana" Font-Size="10pt" Height="166px" OnAuthenticate="Login1_Authenticate"
Width="264px" FailureText="Username ans password did not match!">
<TitleTextStyle BackColor="#6B696B" Font-Bold="True" ForeColor="#FFFFFF" />
</asp:Login>
</div>
</asp:Content>

LoginXML.aspx.cs (C# code file):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
public partial class LoginXML : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
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 xmxdoc = new XmlDocument();
xmxdoc.Load(Server.MapPath("~/App_Data/Loginxml.xml"));
XmlNodeList xmlnodelist = xmxdoc.GetElementsByTagName("User");
foreach (XmlNode xn in xmlnodelist)
{
XmlNodeList xmlnl = xn.ChildNodes;
foreach (XmlNode xmln in xmlnl)
{
if (xmln.Name == "username")
{
if (xmln.InnerText == username)
{
CurrentUser = username;
}
}
if (xmln.Name == "password")
{
if (xmln.InnerText == pwd)
{
CurrentPwd = pwd;
}
}
}
if ((CurrentUser != "") & (CurrentPwd != ""))
{
LoginStatus = true;
}
}
if (LoginStatus == true)
{
Session["UserAuthentication"] = username;
Session.Timeout = 1;
Response.Redirect("Default2.aspx");
}
else
{
Session["UserAuthentication"] = "";
}
} 
}

Output:

Run the application and fill username and password and click login button.

When you click on Login button the page will redirect on other web page.

Download source code
Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics