Form Authentication Form authentication uses cookies to authenticate users and allows the application to do its own credential verification.Using this provider causes unauthenticated requests to be redirected to a specified HTML form using client side redirection.
Forms Authentication Setup
Pages Included : Web.config, Default.aspx, Login.aspx.
Table: create a login table in Sqlserver with columns, login name and password.Insert values in the table.
Web.config Page The Web.config contains all of the configuration settings for an ASP.NET application.
< authentication mode="Forms"> < forms name="cook" protection="All" loginUrl="login.aspx" timeout="30" path="/"/> /authentication>
< authorization> < deny users="?"/> < /authorization>
here - cook is the name of the cookie used for authentication. - Path is the Path used for cookie."/" is the default path. - loginUrl is the URL that unauthenticated users are redirected to. - Protection is Method used to protect cookie data.The value can beDefault or All.Suggested value is "All," which does validation and encryption. - Timeout is the Number of minutes until authentication cookie expires.
In the authorization section, we want to ensure that no unauthenticated users can access the application. The "?" means anonymous users, so we set a deny flag for all anonymous users.
Login.aspx Page:
< form id="Form1" method="post" runat="server"> < asp:TextBox id="user" runat="server" Width="161px" Height="33px"/> < asp:TextBox id="pwd" runat="server" Width="163px" Height="36px"/> < asp:Button id="submit" runat="server" Width="78px" Height="43px" Text="Button"/> < /form>
//declare the required variable
private void submit_Click(object sender, System.EventArgs e) { cn = new SqlConnection("server=pushpa;database=leela;uid=sa;pwd=sql"); cmd = new SqlCommand("Select name,password from login",cn); cn.Open(); dr = cmd.ExecuteReader(); while (dr.Read()) { if((dr.GetString (0)== user.Text )&&(dr.GetString(1)== pwd.Text )) { FormsAuthentication.RedirectFromLoginPage(user.Text,true); Response.Redirect("default.aspx"); } }
}
We can use a button to signout from form authentication and redirect to the login page.
FormsAuthentication.SignOut(); Response.Redirect("logon.aspx");
default.aspx If the user is a valid user then the user is redirected to the default page.
|
No responses found. Be the first to respond and make money from revenue sharing program.
|