ASP.NET Forms Authentication - Subsequent logins redirected to wrong page

Posted on 2008-06-13
Last Modified: 2010-04-21
I have an ASP.NET 3.5 application that uses Forms Authentication.  When first launching a browser, everything works correctly (login, access protected pages, logout).

After SignOut(), no user can use the app without closing the browser (tested IE and Safari).  Specifically, after the next user logs in, the response is redirected to the app's public home page and not to the private page.  

   - I've verified that the auth cookie is no longer present.
   - The code path is the same in both cases
   - I do use a Login control but I manually control the login process (auth, ticket, and redirect)
   - At login, the Session ID is changed
   - This behavior exists regardless of whether I am using a developer server or a production server

Everything appears to be correct but the user is always redirected to the app's home page.  (I have a default.aspx file that is not used; it performs a redirect on load but this code is never called)

I use the FormsAuthentication SignOut() routine, I Abandon() the session, I've verified that my cookies and the auth cookie are no longer present, and I've checked the Response redirect URL -  What else controls the final page after login?  And, since the session is new, what else does the server manage that could interfere with authenticating a second user?
Question by:RobMinnis
  • 8
  • 7
LVL 33

Expert Comment

ID: 21781240
Is there a ReturnURL set in the Querystring on the first request that isn't there on the second?  When a default URL isn't set, will redirect you to default.aspx in the root directory.

You might want to try setting defaultUrl in web.config (same place you set the logonUrl) and see what happens.

Author Comment

ID: 21781341
The DefaultURL is the same in both cases and I override the default with an explicit redirect.  As mentioned, the default.aspx file is present but is not accessed.
LVL 33

Expert Comment

ID: 21781426
Can you post your web.config, specifically the <forms section, and the code that handles your Login?
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.


Author Comment

ID: 21781750
Login ----------------------------------------------------------------------------------------------------------
System.Web.Security.FormsAuthentication.SetAuthCookie(name, li_Login.RememberMeSet);
Response.Redirect("~/General/Home.aspx", false);

Logout ----------------------------------------------------------------------------------------
Response.Redirect("~/Public/Main.aspx"), true);

Web Config ------------------------------------------------------------------------------
<?xml version="1.0"?>
            <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
                  <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
                        <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                        <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
                              <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
                              <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                              <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                              <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/></sectionGroup></sectionGroup></sectionGroup></configSections><appSettings/>
            Set compilation debug="true" to insert debugging
            symbols into the compiled page. Because this
            affects performance, set this value to true only
            during development.
            <compilation debug="true">
                        <add assembly="System.Core, Version=, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                        <add assembly="System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                        <add assembly="System.Xml.Linq, Version=, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                        <add assembly="System.Data.DataSetExtensions, Version=, Culture=neutral, PublicKeyToken=B77A5C561934E089"/></assemblies></compilation>
            The <authentication> section enables configuration
            of the security authentication mode used by
            ASP.NET to identify an incoming user.
            <authentication mode="Forms">
                  <forms loginUrl="Special/login.aspx" name=".EAGLEEYES" requireSSL="false" defaultUrl="General/Home.aspx" cookieless="AutoDetect" protection="All"/>
                  <allow users="?"/>
            The <customErrors> section enables configuration
            of what to do if/when an unhandled error occurs
            during the execution of a request. Specifically,
            it enables developers to configure html error pages
            to be displayed in place of a error stack trace.

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
                        <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                        <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/></controls></pages>
                  <remove verb="*" path="*.asmx"/>
                  <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                  <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                  <add verb="GET,HEAD" path="ScriptResource.axd" validate="false" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/></httpHandlers>
                  <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/></httpModules></system.web>
      <location path="General">
                        <deny users="?"/>
                        <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CSharp.CSharpCodeProvider,System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="4">
                              <providerOption name="CompilerVersion" value="v3.5"/>
                              <providerOption name="WarnAsError" value="false"/></compiler></compilers></system.codedom>
                  <validation validateIntegratedModeConfiguration="false"/>
                  <remove name="ScriptModule"/>
                  <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/></modules>
                  <remove name="WebServiceHandlerFactory-Integrated"/>
                  <remove name="ScriptHandlerFactory"/>
                  <remove name="ScriptHandlerFactoryAppServices"/>
                  <remove name="ScriptResource"/>
                  <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                  <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                  <add name="ScriptResource" verb="GET,HEAD" path="ScriptResource.axd" preCondition="integratedMode" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/></handlers></system.webServer>
            <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
                        <assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/>
                        <bindingRedirect oldVersion="" newVersion=""/></dependentAssembly>
                        <assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/>
                        <bindingRedirect oldVersion="" newVersion=""/></dependentAssembly></assemblyBinding></runtime></configuration>

LVL 33

Expert Comment

ID: 21781818
Interesting authorization section, typically I see this,

                  <deny users="?" />
                  <allow users="*"/>

This denies unauthenticated users, and allows anyone left.

My guess is the Login control is ultimately your problem, since it tries to do a lot of things automatically for you.  Have you tried to use a completely manual process?

Author Comment

ID: 21781964
Actually, the login control only handles data collection - the authenticate event is used if the Login button is clicked but, even in that case, it calls the same authentication code (the only difference is that the event's authenticate flag is set).  Regardless of the path, though, I get the same behavior.

The confusion on the auth section may be that there are two different ones.  The first is for the publicly accessible areas and the second is to protected the folder structure that contains the protected files.
LVL 33

Expert Comment

ID: 21782014
And you're sure the Authenticate method is being called on the second login attempt?  Response.Redirect is a pretty solid method, and once you call it, there isn't a way to undo it, or for futher code to stop it.

Author Comment

ID: 21782122
Everything goes through my Authenticate() and, yes, I have verified that it is always called.  My guess is that either the authentication is not being recorded somewhere in the internals of FormsAuthentication, that there is some mechanism that is overriding the Redirect (my code terminates after this call so I'm not directly overiding it and, based on the call stack, the same chain of events occurs up to that point in both cases), or that some state is inconsistent after the logout.
LVL 33

Accepted Solution

raterus earned 500 total points
ID: 21782199
This site isn't publically accessible that you could give a test username/password is there?

My next course of action is to trace the HTTP Headers of the page and figure out exactly how your browser come to this page, and what page is sending you there.  I can look at it in 1 minute and have what I need, but I can talk you through it as well.

I use these programs to trace,

LiveHTTPHeaders in Firefox


IEHttpHeaders in IE

Download whichever is most convenient, install, and get your page to the login page.  Start the trace, login like you normally would, and paste the results.  Repeat it again after you've logged off and are at the login page again.

Author Comment

ID: 21782599
After tracing the headers and not seeing anything unusual, I went back at looked at the entire process more closely and finally found the cause of the problem.  There are a set of cookies still being passed around that I thought were deleted.  These cookies direct the app to logout the user (go figure).

So, the question is now "how do I get rid of these dang cookies once I don't need them?"  I thought that Response.Cookies.RemoveAll() would do that but these, in effect, originate at the client (i.e. passed back) and so they are not in the Response list.  I also tried removing them from the Request assuming that this would prevent them from being populated back into the Response but that didn't work.

I can set a short expiration and work around the problem but I'd prefer not to play Russian Roulette with timing.  What is the correct way to get rid of cookies?
LVL 33

Expert Comment

ID: 21782678
Use javascript, I've written javascript in a many logout pages to actually delete the cookies, because I noticed Response.Cookies.RemoveAll didn't work at all.

Author Comment

ID: 21782833
Would you happen to have JS handy to delete them?
LVL 33

Expert Comment

ID: 21782877
It's as simple as setting the expiration to a past date.  This immediately deletes it.

function del_cookie(name) {
document.cookie = name +
'=; expires=Thu, 01-Jan-70 00:00:01 GMT;';

Author Closing Comment

ID: 31467004
Thanks for the help.

Author Comment

ID: 21783490
Just to follow-up:  The final solution was not to set the expiration via javascript.  Using this technique in IE, the JS actually created a second cookie rather than setting the expiration on the first.  In Safari, it did nothing at all.

The final solution was to set the expiration on the server *after* processing the cookie (i.e. the final segment of the roundtrip from server->client->server) and then to manually add the cookie into the Response object.
   - if you set the expiration at the first step, it will not work if the client's and server's clocks are not synced.  I also added a server-side check to see if the cookie has expired incase the client's clock is out of sync enough that the cookie doesn't expire after another round trip.
   - if you do not manually add the cookie into the Response, the changes to the expiration will not propogate

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit If you want to manage em…

803 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