IN web.config file, add this
system.web>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="Forms">
<forms loginUrl="~/LoginPage.aspx" timeout="2880"></forms>
</authentication>
<authorization>
<deny users="?"/>
<allow users="*"/>
</authorization>
</system.web>
Remember always "deny user" tag first then followed by "allow User" tag.
Add a new webPage name it Login.aspx. Add two text boxes for user name and Password. Add One Button and one Lable to show error if wrong password.
In Login.aspx.cs file, write this code in button1_click event
using System.Web.Security;
using System.Drawing;
protected void Button1_Click(object sender, EventArgs e)
{
string usr = TextBox1.Text.Trim().ToUpper();
string psswrd = TextBox2.Text;
if (usr == "123" && psswrd == "123")
{
FormsAuthentication.SetAuthCookie(usr, false);
string retrnUrl = Request.QueryString["returnUrl"];
if (!string.IsNullOrEmpty(retrnUrl))
{
Response.Redirect(retrnUrl);
}
else
{
Response.Redirect("HomePage.aspx");
}
}
else
Label1.Text = "User name or password is wrong";
Label1.ForeColor = Color.Red;
}
You are done..!
Try to open any page, it will show you Login Page. If and only if login success, then it will redirect you to original requested page.. Hurrey.. enjoy..!!
You can use below code to sign out.
FormsAuthentication.SignOut();
Response.Redirect("Login.aspx");