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 /> </div>Password: password<br /> <table width="375"> <tr> </table><td align="left" colspan="2" style="height: 21px">Please Login:</td> </tr><tr> <td align="right" style="width: 30%">Username:</td> </tr><td align="left" style="width: 70%"> <asp:TextBox ID="txtUsername" runat="server"></asp:TextBox></td> <tr> <td align="right" style="width: 30%">Password:</td> </tr><td align="left" style="width: 70%"> <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox></td> <tr> <td align="center" colspan="2"> </tr><asp:Button ID="btnLogin" runat="server" OnClick="btnLogin_Click" Text="Login" /> </td> <tr> <td align="left" colspan="2"> </tr><asp:Literal ID="litUserData" runat="server"></asp:Literal></td> </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 End Class Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogin.Click Select Case UserLogin(txtUsername.Text, txtPassword.Text) End SubCase 1 Session.Abandon() Login() Case 2 litUserData.Text = "Bad Password" Case 3 End SelectlitUserData.Text = "Unknown User" Public Sub Login() Dim objTicket As FormsAuthenticationTicket = Nothing End SubDim 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 IfPublic Function UserLogin(ByVal strUsername As String, ByVal strPassword As String) As Integer Dim iReturnValue As Integer = 0 End FunctionDim 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 |
No comments:
Post a Comment