Solved

response.redirect same page

Posted on 2010-09-21
10
1,340 Views
Last Modified: 2012-05-10
if (!Page.IsPostBack)
            {
               

                if (!string.IsNullOrEmpty((string)Session["UserName"]))
                {

                    strVal = ((string)Session["UserName"]);

                    if (AuthenticateNTUsers(strVal) == true)
                    {
                        Response.Redirect("~/mypage.aspx", false);
                    }
                    else
                    {
                        Response.Redirect("~/login.aspx", false);
                       
                    }
                }
}
now when ever session is empty it is supposed to redirect to the login page and display the log in form, but for some reason its going into infinite loop. I guess because each redirect is not oseen as poast back. Any ideas as to how to resolve this?
0
Comment
Question by:Angela4eva
10 Comments
 

Author Comment

by:Angela4eva
ID: 33730866
above code is part of login.aspx page as well
0
 
LVL 10

Expert Comment

by:himanshut
ID: 33730979
if (!Page.IsPostBack)
            {
               

                if ((Session["UserName"].ToString()=="") || (Session["UserName"].ToString()==null))
                {

                    strVal = ((string)Session["UserName"]);

                    if (AuthenticateNTUsers(strVal) == true)
                    {
                        Response.Redirect("~/mypage.aspx");
                    }
                    else
                    {
                        Response.Redirect("~/login.aspx");
                       
                    }
                }
               else
                    {
                        Response.Redirect("~/login.aspx");
                       
                    }
}
0
 

Author Comment

by:Angela4eva
ID: 33731025
it stil going in infinite loop. I think it has to do with the back that response.redirect does not poastback. Any other ideas
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 10

Accepted Solution

by:
himanshut earned 250 total points
ID: 33731031
Oops.. it was otherway around :)
if (!Page.IsPostBack)
            {
               

                if ((Session["UserName"].ToString()=="") || (Session["UserName"].ToString()==null))
                {

                    Response.Redirect("~/login.aspx");
                       
                }
               else
                    {
                       strVal = ((string)Session["UserName"]);

                    if (AuthenticateNTUsers(strVal) == true)
                    {
                        Response.Redirect("~/mypage.aspx");
                    }
                    else
                    {
                        Response.Redirect("~/login.aspx");
                       
                    }
                        
                    }
}

Open in new window

0
 
LVL 10

Expert Comment

by:himanshut
ID: 33731082
in the previous code, I mistakenly put wrong code in if else conditions:

In the above code:
If Session is Empty or NULL it will redirect to login.aspx
otherwise it will continue the normal processing.

Can you put a breakpoint and debug it line by line to see if it comes into the if condition, also what is the value for the Session["UserName"].ToString().
Does it has username string value or not.

Cheers!
0
 

Author Comment

by:Angela4eva
ID: 33731138
yes its coming to if but let me tell you that this code itself is in login.aspx and so when i do response.redirect it is not causing postback its executing it everytime
0
 
LVL 20

Expert Comment

by:informaniac
ID: 33731417
Can you tell us the design of the login page? And why do you want to do the authentication on page load
0
 
LVL 10

Assisted Solution

by:codingbeaver
codingbeaver earned 250 total points
ID: 33735990
Remove your "else" block in your code.
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 33736519
--->above code is part of login.aspx page as well

If it is the login page, you don't need to Redirect to login.aspx at all. If you don't redirect, it will stay on that page.
Or is it that you are using a BasePage class  that the login inherits from or master page Load?

If that is the case then you can check if it is request from Login.aspx and if so then don't do a redirect like:

string filename = VirtualPathUtility.GetFileName(Request.Path);
                if (filename != "Login.aspx")
                {
                    Response.Redirect("~/Login.aspx");
                }

Check this: http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_26466964.html
0
 

Author Closing Comment

by:Angela4eva
ID: 33771437
thanks
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question