Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
Tutorial RSS
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Charts
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Website Navigation - Using Forms Authentication Ticket in ASP.NET and VB
Using Forms Authentication Ticket in ASP.NET and VB

ASP.Net 4.0 Tutorials V4
Server Intellect Cloud Hosting

ASP.NET Website Navigation Tutorial

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!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!



 
  Developer Resources







Server Intellect Rocks