Articles: 843 | Categories: 148   
   
   
Home Articles Contact Us
 
 
 
 
Using Forms Authentication Ticket in ASP.NET and C# (0 Comments)
Admin: Posted Date: April 4, 2010

Implementing login on your website with Forms Authentication Ticket. C# version.An easy way to create user login features on your website is to make use of the Forms Authentication Ticket in ASP.NET

Using Forms Authentication Ticket in ASP.NET and C#

Implementing login on your website with Forms Authentication Ticket. C# version.

An easy way to create user login features on your website is to make use of the Forms Authentication Ticket in ASP.NET
We can do this quite simply, and we start off by including the assembly reference:

using System.Data.SqlClient;

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.


 

The ASPX page will look something like this:


User: user

Password: password







Please Login:
Username:

Password:

 


Finally, we create the following methods to handle the login form.
The code-behind will look something like this:


 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnLogin_Click(object sender, EventArgs e)
{
switch (UserLogin(txtUsername.Text, txtPassword.Text))
{
case 1:
Session.Abandon();
Login();
break;
case 2:
litUserData.Text = "Bad Password";
break;
case 3:
litUserData.Text = "Unknown User";
break;
}
}

public void Login()
{
FormsAuthenticationTicket objTicket = null;
HttpCookie objCookie = null;
string strReturnURL = null;
objTicket = new FormsAuthenticationTicket(1, txtUsername.Text, System.DateTime.Now, DateTime.Now.AddMinutes(60), false, Session.SessionID);
objCookie = new HttpCookie(".ASPXAUTH");
objCookie.Value = FormsAuthentication.Encrypt(objTicket);
Response.Cookies.Add(objCookie);
strReturnURL = Request.QueryString["ReturnURL"];
if (strReturnURL != null)
Response.Redirect(strReturnURL);
else
Response.Redirect("Default2.aspx", false);
}

public int UserLogin(string strUsername, string strPassword)
{
int iReturnValue = 0;

SqlConnection con1 = new SqlConnection(ConfigurationManager.AppSettings["ConnString"]);
SqlCommand cmd = new SqlCommand("spAuthAdminUser", con1);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add("@username", strUsername);
cmd.Parameters.Add("@password", strPassword);
cmd.Parameters.Add("@ReturnValue", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
con1.Open();
cmd.ExecuteNonQuery();
iReturnValue = System.Convert.ToInt32(cmd.Parameters["@ReturnValue"].Value.ToString());
con1.Close();
return iReturnValue;
}
}

 

 

 
 
Add a Comment:
 
(You must be signed in to comment on an article. Not a member? Click here to register)
   
Title:

Comments: