Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi everybody ,,

i want to ask about using master page. i am work on archiving web application so i lad web application with master page contains rooted menu to different pages. i want to force log in before accessing any page in the application
Posted

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

C#
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.

C#
FormsAuthentication.SignOut();
            Response.Redirect("Login.aspx");
 
Share this answer
 
v2
Comments
Badour alsamaraie 9-Feb-15 3:57am    
why using cookies? actually i did it but no change, it still appear master page first when i am run the project!!!
Shridhar Gowda 9-Feb-15 4:06am    
Is Web.Config file is Outside of all WebPages? I think, the file you are referring to is outside of the web.config's boundary.
Badour alsamaraie 10-Feb-15 14:46pm    
when i use the code below and but my defualt.aspx as start page login.aspx not run first!!

protected void Button1_Click(object sender, EventArgs e)
{

if(( arch.Users.Any(u=>u.username == TextBox1.Text))==true)
{
var get_user_pass= (from x in arch.Users where x.username==TextBox1.Text
select x.password).FirstOrDefault();
//if ((TextBox1.Text == "1") && (TextBox2.Text == "1"))
if(Convert.ToString(get_user_pass)==TextBox2.Text)
{
FormsAuthentication.SetAuthCookie(TextBox1.Text, false);
string returnUrl = Request.QueryString["returnUrl"];
if (!string.IsNullOrEmpty(returnUrl))
{ Response.Redirect(returnUrl); }
else { Response.Redirect("Default.aspx"); }


}
}
}



what i want is that before loading master page login.aspx appear even if it is not the start application page!?
Badour alsamaraie 10-Feb-15 14:49pm    
when i use the code below login.aspx not run first !!

protected void Button1_Click(object sender, EventArgs e)
{

if(( arch.Users.Any(u=>u.username == TextBox1.Text))==true)
{
var get_user_pass= (from x in arch.Users where x.username==TextBox1.Text
select x.password).FirstOrDefault();
//if ((TextBox1.Text == "1") && (TextBox2.Text == "1"))
if(Convert.ToString(get_user_pass)==TextBox2.Text)
{
FormsAuthentication.SetAuthCookie(TextBox1.Text, false);
string returnUrl = Request.QueryString["returnUrl"];
if (!string.IsNullOrEmpty(returnUrl))
{ Response.Redirect(returnUrl); }
else { Response.Redirect("Default.aspx"); }


}
}
}


what i want is to make my defualt.aspx(which is inside master page) as start page of application, but login.aspx must in force so user must login before using the master page
Shridhar Gowda 11-Feb-15 1:46am    
<forms loginUrl="~/LoginPage.aspx" timeout="2880"></forms> This line in web.config means, User can only access any .aspx page in application if and only if he crossed LoginPage.aspx successfully. Here .Master pages are nothing to do with authentication. Authentication is applicable to .aspx pages. Please refer below link for more info. http://www.codeproject.com/Tips/874663/How-to-use-Form-authentication-in-ASP-NET-and-MVC

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900