Form authentication with backend database : Form Based « Authentication Authorization « ASP.NET Tutorial






<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>

<script language="VB" runat="server">
   sub Login(Sender as Object, e as EventArgs) 
      dim intID as integer = 0

      dim Conn as new OleDbConnection("Provider=" & _
            "Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=userTable.mdb")

      dim objCmd as OleDbCommand = new OleDbCommand _
         ("SELECT UserID FROM tblUsers WHERE " & _
         "Username = '" & tbUserName.Text & "' " & _
         "AND Password = '" & tbPassword.Text & "'", Conn)
      dim objReader as OleDbDataReader

      try
         objCmd.Connection.Open()
         objReader = objCmd.ExecuteReader()

         do while objReader.Read
            intId = objReader.GetInt32(0).ToString()
         loop
      catch ex as OleDbException
         lblMessage.Text = ex.Message
      finally
         objReader.Close()
         objCmd.Connection.Close()
      end try

      if intID <> 0 then
         FormsAuthentication.SetAuthCookie(intID, false)
         lblMessage.Text = "Success!"
      else
         lblMessage.Text = "Invalid username or password!"
      end if
   end sub      
</script>

<html><body>
   <form runat="server">
      <asp:Label id="lblMessage" runat="server"/>
      Username:
      <asp:Textbox id="tbUsername" runat="server" /><br>
      Password:
      <asp:Textbox id="tbPassword" TextMode="password" runat="server" />
      <asp:Button id="Submit" runat="server" onClick="Login" text="Submit" />
   </form>
</body></html>


File: Web.Config

<configuration>
   <system.web>
      <authentication mode="Forms">
         <forms name="AuthCookie" loginUrl="login.aspx" />
      </authentication>
      <authorization> 
         <deny users="?"/> 
      </authorization> 
   </system.web>
</configuration>








21.9.Form Based
21.9.1.Create a new folder in your application named SecretFiles
21.9.2.Customizing the Login form
21.9.3.Automatically Redirecting a User to the Referring Page
21.9.4.Form authentication with backend database