.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Form Authentication Tutorial

Posted By:Lakhan Pal Garg       Posted Date: September 12, 2009    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  
 

Form Authentication Tutorial. In this we will see the code how to implement form authentication in asp.net.

aspx.cs File's Content:

On Button Click for Login
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "server={ServerName};uid={UserID};pwd={Password};database={DatabaseName}";
con.Open();
SqlCommand cm = new SqlCommand("select roles from users where username = '" + TextBox1.Text + "' and password = '" + TextBox2.Text + "'", con);
SqlDataReader dr = cm.ExecuteReader();
if (dr.Read())
{
FormsAuthenticationTicket tk = new FormsAuthenticationTicket(1, TextBox1.Text, DateTime.Now, DateTime.Now.AddMinutes (1),true, dr.GetString(0),FormsAuthentication.FormsCookiePath);
string hash = FormsAuthentication.Encrypt(tk);
HttpCookie Logincookie = new HttpCookie(FormsAuthentication.FormsCookieName,hash);
Response.Cookies.Add(Logincookie);
if (tk.IsPersistent)
{
HttpCookie ck = new HttpCookie( FormsAuthentication.FormsCookieName, hash);
ck.Expires = tk.Expiration;
Response.Cookies.Add(ck);
}
string returnUrl = Request.QueryString["ReturnUrl"];
if (returnUrl == null) returnUrl = "Main.aspx";
Response.Redirect(returnUrl);
}
else
{
Response.Write("The Username / Password not valid !");
}
con.Close();
dr.Close();
cm.Dispose();
}

Add These lines of code in Global.asax:
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
if (HttpContext.Current.User != null)
{
if (HttpContext.Current.User.Identity.IsAuthenticated )
{
if (HttpContext.Current.User.Identity is FormsIdentity)
{
FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
FormsAuthenticationTicket ticket = id.Ticket;
HttpCookie ck = new HttpCookie("ticket");
string userData = ticket.UserData;
string[] roles = userData.Split(',');
HttpContext.Current.User = new GenericPrincipal(id, roles);
}
}
}
}


Database:
CREATE TABLE users
(
username nvarchar(64) CONSTRAINT users_PK PRIMARY KEY,
password nvarchar(12,
roles nvarchar(64)
)
CREATE INDEX credentials ON users
(
username,
password
)
insert into users values('client','client','c')
insert into users values('admin','admin','a')
select * from users


Add These Lines in Web Config
Before







After & before


















 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend