Solved

response.redirect same page

Posted on 2010-09-21
10
1,316 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
Comment Utility
above code is part of login.aspx page as well
0
 
LVL 10

Expert Comment

by:himanshut
Comment Utility
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
Comment Utility
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
 
LVL 10

Accepted Solution

by:
himanshut earned 250 total points
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:Angela4eva
Comment Utility
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
Comment Utility
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
Comment Utility
Remove your "else" block in your code.
0
 
LVL 41

Expert Comment

by:guru_sami
Comment Utility
--->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
Comment Utility
thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now