Solved

response.redirect same page

Posted on 2010-09-21
10
1,331 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Memory Usage 2 50
About delegates in c sharp 3 31
Exit the loop 4 34
Adware on IIS hosted asp.net website 1 15
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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 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

16 Experts available now in Live!

Get 1:1 Help Now