Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HEllo everyone,

I have a problem with the redirecton after a seccesful login. I am trying this code
C#
private void imgBtnLogin_Click(object sender, ImageClickEventArgs e)
   {
      FormsAuthentication.Initialize();
       SqlConnection con = new SqlConnection("data source=.; initial catalog = AxaStock; integrated security = true");
       SqlCommand cmd = con.CreateCommand();
       cmd.CommandText = "select r.nomRole from Collaborateur c, Role r where r.idRole=c.idRole and matricule=@matricule and password=@password";
       cmd.Parameters.Add("@matricule", SqlDbType.VarChar, 64).Value = txtLogin.Text;
       cmd.Parameters.Add("@password", SqlDbType.VarChar, 128).Value = txtPassword.Text;

       con.Open();
       SqlDataReader reader = cmd.ExecuteReader();
       if (reader.Read())
       {
           FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, txtLogin.Text,
               DateTime.Now,
               DateTime.Now.AddMinutes(30),
               true,
               reader.GetString(0),
               FormsAuthentication.FormsCookiePath);
           string hash = FormsAuthentication.Encrypt(ticket);
           HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
           if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;
           Response.Cookies.Add(cookie);
           string returnUrl = Request.QueryString["ReturnUrl"];


           if (returnUrl == null)
           {
               returnUrl = "/";

           }
       }
       else
       {
           lblError.Text = "Matricule / mot de passe incorrect. Réssayez !";
           lblError.Visible = true;
       }
       reader.Close();
       con.Close();


   }


When I click the button login, it doesnt redirect me to the specific folder, it Stays in the Login page without giving me any message. How can I tell him to redirect me to the Accueil.aspx ??
please help
Posted
Comments
Prasad Avunoori 23-Jun-14 6:13am    
Put try, catch block and debug it.
Nandakishore G N 23-Jun-14 9:08am    
are you validating for sessions in accuiel.aspx page. If yes, that is the reason for redirecting back to login page. check once.

Please use below code for redirection,after successfull login.
Response.Redirect("...page path.../Accueil.aspx");
 
Share this answer
 
Comments
Member 10889990 23-Jun-14 6:29am    
I did, and it gives me this URL
http://localhost:59887/AxaStock/Default2.aspx?ReturnUrl=%2fAxaStock%2fTP%2fAccueil.aspx
and yet it doesn't redirect me to the accueil.aspx, it stays in login page !!
manak chand 23-Jun-14 6:38am    
please specify, what page path you are using.. pls debug and let us know..
You have to use Response.Redirect(), at the end of your method, like in the next example:
Response.Redirect("Accueil.aspx");
 
Share this answer
 
Hi,

1. returnUrl should be the landing page after your login (try '/welcome.aspx' instead of '/')

2. Response.Redirect(returnUrl);
reader.Close();
con.Close();
 
Share this answer
 
Comments
Member 10889990 23-Jun-14 6:55am    
I tried that and didn't work :'(
Member 10889990 23-Jun-14 7:41am    
@Karthick Viswanthan The structure of my project :
There is the root, login.asp, web.config, Global.asax, contact.aspx, default.aspx(an empty page) and there is folders named by roles : a folder "TP", "CDP", "GS" each one of these folders have it's own welcome.aspx page. When the user clicks on login button , it must redirect to the TP/welcome.aspx or GS/welcome.aspx
The problem is that you use
C#
FormsAuthentication.Initialize();

when entering your method...
According to MSDN:
This method is not intended to be called from your code.

You actually clear FormsAuthentication so you are never authenticated and stayed on the login page...
 
Share this answer
 
Comments
Member 10889990 23-Jun-14 6:55am    
I took it off, but the problem is still here :(
Kornfeld Eliyahu Peter 23-Jun-14 7:14am    
Looking closer to your code, there is no code to do the redirect at all...
Use FormsAuthentication.RedirectFromLoginPage([user_name], true); and do not bother with cookie creation by yourself...
http://msdn.microsoft.com/en-us/library/xdt4thhy(v=vs.85).aspx
Member 10889990 23-Jun-14 7:30am    
LEmme explain to you the structure of my project There is the root, login.asp my web.config, Global.asax, contact.aspx, default.aspx(an empty page) and there is folders named by roles : a folder "TP", "CDP", "GS" each one of these folders have it's own welcome.aspx page. When the user clicks on login button ,and by adding the method you told me, it redirects to the default.aspx page, which is not the page I want, in the matter fact it must redirect me to the TP/welcome.aspx
Kornfeld Eliyahu Peter 23-Jun-14 12:41pm    
The problem is that you try to solve problems (like ticket-to-cookie) in your code, but form authentication in ASP.NET provides you all that out-of-the-box! Please go and learn how to implement form authentication...

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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