Monday, 25 July 2011

Using Forms Authentication Ticket in ASP.NET and VB


Implementing login on your website with Forms Authentication Ticket. VB 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:

Imports System.Data.SqlClient
Imports System.Data

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

The ASPX page will look something like this:

<form id="form1" runat="server">
<div align="center">
User: user<br />
Password: password<br />
<table width="375">
<tr>
<td align="left" colspan="2" style="height: 21px">Please Login:</td>
</tr>
<tr>
<td align="right" style="width: 30%">Username:</td>
<td align="left" style="width: 70%">
<asp:TextBox ID="txtUsername" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td align="right" style="width: 30%">Password:</td>
<td align="left" style="width: 70%">
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox></td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:Button ID="btnLogin" runat="server" OnClick="btnLogin_Click" Text="Login" /> </td>
</tr>
<tr>
<td align="left" colspan="2">
<asp:Literal ID="litUserData" runat="server"></asp:Literal></td>
</tr>
</table>
</div>
</form>

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

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

Imports System.Data.SqlClient
Imports System.Data

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogin.Click
Select Case UserLogin(txtUsername.Text, txtPassword.Text)
Case 1
Session.Abandon()
Login()
Case 2
litUserData.Text = "Bad Password"
Case 3
litUserData.Text = "Unknown User"
End Select
End Sub

Public Sub Login()
Dim objTicket As FormsAuthenticationTicket = Nothing
Dim objCookie As HttpCookie = Nothing
Dim strReturnURL As String = Nothing
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 IsNot Nothing Then
Response.Redirect(strReturnURL)
Else
Response.Redirect("Default2.aspx", False)
End If
End Sub

Public Function UserLogin(ByVal strUsername As String, ByVal strPassword As String) As Integer
Dim iReturnValue As Integer = 0

Dim con1 As New SqlConnection(ConfigurationManager.AppSettings("ConnString"))
Dim cmd As 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
End Function
End Class

Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment