Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

1 solution

Use FormsAuthentication and set the LoginUrl to your Login.aspx page. Something like:

XML
<authentication mode="Forms">
    <forms loginUrl="~/Login.aspx" protection="All" timeout="600" slidingExpiration="true" domain=""/>
  </authentication>


Then after they login set the authentication cookies using FormsAuthentication.RedirectFromLoginPage().

This way any request to your site if they have not been authenticated they will be sent automatically to Login.aspx.
 
Share this answer
 
Comments
Badour alsamaraie 10-Feb-15 15:19pm    
how can i set authentication cookies using FormsAuthentication.RedirectFromLoginPage() ??
ZurdoDev 10-Feb-15 15:29pm    
It does it automatically. Do you need to overwrite it for some reason?

You can do it with something like:

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, UserID, DateTime.Now, DateTime.Now.AddMinutes(HttpContext.Current.Session.Timeout), false, UserID, FormsAuthentication.FormsCookiePath);

string encTicket = FormsAuthentication.Encrypt(ticket);

// Create the cookie.
HttpCookie cook = new HttpCookie(FormsAuthentication.FormsCookieName);
cook.Domain = FormsAuthentication.CookieDomain;
cook.Expires = DateTime.Now.AddMinutes(HttpContext.Current.Session.Timeout);
cook.Value = encTicket;

HttpContext.Current.Response.AppendCookie(cook);

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