views:

196

answers:

3

I get the strange error "Object moved to here." on a blank white page when I try to login on a site I created. It works fine locally but not when I deploy it to the test or production server. I am not doing anything odd, just using basic authentication code in a helper function as follows...

public static bool AuthenticateUser(string Username, string Password, bool PersistLogin, string RedirectionURL)
    {
        if (Membership.ValidateUser(Username, Password))
        {
            FormsAuthentication.SetAuthCookie(Username, PersistLogin);                
            HttpContext.Current.Response.Redirect(RedirectionURL,true);
            return true;
        }
        else
            return false;
    }
A: 

A quick Google search revealed this KB article with a fix.

Josh Stodola
Google was my first port of call, and I also tried that fix and a few others - so I'm here now.
Dkong
And how am I supposed to know what you have tried? Perhaps you should include that in your question!
Josh Stodola
Most developers would go to Google first - it's a given, and preferable to asking for help.
Dkong
Do you have SP1 installed on the server?
Josh Stodola
Dkong you are giving "most developers" too much credit. :|
Bryan
A: 

I think it is because page redirect to other page before return true. So "Object moved to here".

Allen Le
+1  A: 

You need to use RedirectFromLoginPage in this context... for exactly this reason.

If you end response processing immediately, the auth cookie may not get set.

Bryan
Thanks for the answer. I understand that RedirectFromLoginPage method will send the user to the Default login or the originally requested URL. If they are logging in from scratch though (ie on the front page of the site), I was hoping to specify the location they should be routed to, ie Admin page, or User page (which I would determine by checking their roles). Is there a way to override the Default redirect location?
Dkong
OK. Why are you telling Response.Redirect to end processing immediately? Have you tried Response.Redirect(RedirectionURL, false)?This also means that AuthenticateUser is not returning. Why not finish processing first? Is anything relying on this method's return?
Bryan
I was using Response.Redirect because the "RedirectionURL" method I use is a custom method which gets the user's context and determines where to send them. I did this because I was having a hard time trying to figure out how to override the RedirectFromLogin page. I have also tried passing in 'false' for the EndResponse parameter.
Dkong
Well, I'm not sure what else to tell you here my friend. Bottom line, you need to use RedirectFromLoginPage. Go to a temporary "Redirecting you..." page if you need to and add a meta-refresh or some javascript to then get the user to your target page.
Bryan
Hi Bryan. Thanks for your ideas. I'll give it a go.
Dkong