Solved

response.redirect same page

Posted on 2010-09-21
10
1,359 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

733 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