{"id":1990,"date":"2012-03-30T15:12:12","date_gmt":"2012-03-30T19:12:12","guid":{"rendered":"http:\/\/www.iowawebnet.com\/ein\/?p=1990"},"modified":"2012-04-02T14:23:12","modified_gmt":"2012-04-02T18:23:12","slug":"vb-net-login-form-create-user-session","status":"publish","type":"post","link":"https:\/\/www.iowawebnet.com\/ein\/2012\/03\/vb-net-login-form-create-user-session\/","title":{"rendered":"VB.NET &#8211; Login form &#8211; create user session"},"content":{"rendered":"<p>This post will guide you through designing the login form with SQL backend and creating a user session.<br \/>\n<br \/>\n<a href=\"http:\/\/www.armslist.com\/contests?utm_source=c000019&#038;utm_medium=plink&#038;utm_campaign=p025264\" target=\"_blank\">Learn more about new promotions<\/a>!<br \/>\n<strong>login.aspx<\/strong><\/p>\n<blockquote>\n<pre>\r\nlogin.aspx (markup code)\r\n<code>\r\n&lt;%@ Page Title=\"\" Language=\"VB\" MasterPageFile=\"~\/Default.Master\" \r\nAutoEventWireup=\"false\" CodeFile=\"login.aspx.vb\" Inherits=\"_login\" %>\r\n&lt;asp:Content ID=\"HeaderContent\" runat=\"server\" \r\nContentPlaceHolderID=\"HeadContent\">\r\n&lt;\/asp:Content>\r\n&lt;asp:Content ID=\"BodyContent\" runat=\"server\" \r\nContentPlaceHolderID=\"MainContent\" >\r\n<!--  comment out start tag here -->\r\n&lt;table border=\"0\" cellpadding=\"1\" cellspacing=\"0\">\r\n   &lt;tr>\r\n      &lt;td colspan=\"2\">Login:&lt;\/td>\r\n   &lt;\/tr>\r\n   &lt;tr>\r\n      &lt;td colspan=\"2>\r\n     &lt;br \/>\r\n     &lt;asp:label ID=\"ssError\" runat=\"server\">&lt;\/asp:label>&lt;\/td>\r\n     &lt;\/tr>\r\n     &lt;tr>\r\n      &lt;td align=\"right\">Username:&nbsp;&lt;\/td>\r\n      &lt;td>\r\n      &lt;asp:textbox ID=\"usn\" runat=\"server\" ToolTip=\"username\" \r\n      CssClass=\"loginBox\" >&lt;\/asp:textbox>\r\n      &lt;asp:requiredfieldvalidator ID=\"RVusn\" runat=\"server\"\r\n      ErrorMessage=\"*\" ControlToValidate=\"usn\" >\r\n      &lt;\/asp:requiredfieldvalidator>&lt;\/td>\r\n   &lt;\/tr>\r\n   &lt;tr>\r\n       &lt;td align=\"right\">Password:&nbsp;\r\n       &lt;td>\r\n       &lt;asp:textbox ID=\"pwd\" runat=\"server\" TextMode=\"Password\" \r\n       ToolTip=\"password\" CssClass=\"loginBox\" >&lt;\/asp:textbox>\r\n      &lt;asp:requiredfieldvalidator ID=\"RVpwd\" runat=\"server\"\r\n      ErrorMessage=\"*\" ControlToValidate=\"pwd\">\r\n      &lt;\/asp:requiredfieldvalidator>&lt;\/td>\r\n   &lt;\/tr>\r\n   &lt;tr>\r\n      &lt;td colspan=\"2\">&nbsp;&lt;\/td>\r\n   &lt;\/tr>\r\n   &lt;tr>\r\n      &lt;td colspan=\"2\" class=\"text\">\r\n      &lt;asp:imagebutton ID=\"Login\" runat=\"server\" \r\n      ImageURL=\"\/images\/buttons\/btn_signin.gif\" \/>\r\n      &lt;\/td>\r\n   &lt;\/tr>\r\n &lt;\/table>\r\n&lt;\/div>\r\n&lt;\/asp:Content>\r\n<\/code>\r\nlogin.aspx.vb (code behind)\r\n<code>\r\nImports System.IO\r\nImports System.Data.OleDb\r\nImports System.Web.UI.WebControls.ImageButton\r\nImports System.Web.UI.WebControls.TextBox\r\nImports System.Web.SessionState\r\nImports System.Web.Security\r\nImports System.Web\r\nPartial Class _ss_login\r\n  Inherits System.Web.UI.Page\r\n   Protected Sub Login_Click(ByVal sender As Object, _\r\n     _ByVal e As System.Web.UI.ImageClickEventArgs)\r\n              Handles Login.Click\r\n     '\/ defining variables\/controls\r\n      Dim usn As TextBox\r\n      Dim pwd As TextBox\r\n\r\n      'binding controls\r\n       usn = Login.FindControl(\"usn\")\r\n       pwd = Login.FindControl(\"pwd\")\r\n\r\n       Dim user As String\r\n       Dim key As String\r\n       user = usn.Text.ToString()\r\n       key = pwd.Text.ToString()\r\n       \r\n   'encrypt password\r\n    Dim sha1Obj As _\r\n    New System.Security.Cryptography.SHA1CryptoServiceProvider\r\n     Dim bytesToHash() As _\r\n       _Byte = System.Text.Encoding.ASCII.GetBytes(key)\r\n    bytesToHash = sha1Obj.ComputeHash(bytesToHash)\r\n\r\n    Dim keyCode As String = \"\"\r\n    For Each b As Byte In bytesToHash\r\n        keyCode += b.ToString(\"x2\")\r\n     Next\r\n\r\n     '\/ sql oledb connection * you can define your own sql connection\r\n     Dim ConnDB As_\r\n       _New OleDbConnection(\"Provider=sqloledb;Data Source=mySqlServer;\r\n     Initial Catalog=mySqlDB;Persist Security Info=True;_\r\n       _User ID=myUsr;Password=myPwd\")\r\n     Dim cmd As New OleDbCommand(\"SELECT * FROM ss_users WHERE \r\n                                (u_user_name = '\" & usn.Text.ToString() & \"')\r\n                                AND\r\n                                (u_password = '\" & keyCode & \"');\", ConnDB)\r\n     Dim reader As OleDbDataReader\r\n\r\n     Try\r\n       '\/ open connection\r\n       cmd.Connection.Open()\r\n       reader = cmd.ExecuteReader()\r\n       If reader.Read() Then\r\n         'create sessions\r\n          Session(\"Name\") = reader(6).ToString()\r\n          Session(\"ID\") = reader(0).ToString()\r\n          Session(\"LastLogIn\") = reader(11).ToString()\r\n          Response.Redirect(\"default.aspx\")  'page of your choice\r\n       Else\r\n          ssError.Text = (\"<font color='red'>Error - please try again.<\/font>\")\r\n       End If\r\n       cmd.Connection.Close()\r\n       Catch ex As Exception\r\n          ssError.Text = \"<font color='red'>Error:<\/font> \" & ex.Message.ToString()\r\n       End Try\r\n    End Sub\r\nEnd Class\r\n<\/code>\r\n<\/pre>\n<\/blockquote>\n<p>Related posts:<br \/>\n<a href=\"http:\/\/www.iowawebnet.com\/ein\/2012\/03\/vb-net-end-user-session\/\">End User Session<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post will guide you through designing the login form with SQL backend and creating a user session. Learn more about new promotions! login.aspx login.aspx (markup code) &lt;%@ Page Title=&#8221;&#8221; Language=&#8221;VB&#8221; MasterPageFile=&#8221;~\/Default.Master&#8221; AutoEventWireup=&#8221;false&#8221; CodeFile=&#8221;login.aspx.vb&#8221; Inherits=&#8221;_login&#8221; %> &lt;asp:Content ID=&#8221;HeaderContent&#8221; runat=&#8221;server&#8221; ContentPlaceHolderID=&#8221;HeadContent&#8221;> &lt;\/asp:Content> &lt;asp:Content ID=&#8221;BodyContent&#8221; runat=&#8221;server&#8221; ContentPlaceHolderID=&#8221;MainContent&#8221; > &lt;table border=&#8221;0&#8243; cellpadding=&#8221;1&#8243; cellspacing=&#8221;0&#8243;> &lt;tr> &lt;td colspan=&#8221;2&#8243;>Login:&lt;\/td> &lt;\/tr> &lt;tr> [&hellip;]<\/p>\n","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-1990","post","type-post","status-publish","format-standard","hentry","category-applications"],"_links":{"self":[{"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/posts\/1990"}],"collection":[{"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/comments?post=1990"}],"version-history":[{"count":37,"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/posts\/1990\/revisions"}],"predecessor-version":[{"id":2027,"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/posts\/1990\/revisions\/2027"}],"wp:attachment":[{"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/media?parent=1990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/categories?post=1990"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/tags?post=1990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}