Tag Archives: path

Set Forms Authentication Path dynamically

Lets say we have IIS with a default website and two Virtual Directories, both containing an instance of an application. So when a user logs into Instance A and then into Instance B, he is then logged out of Instance A. In testing I saw that if I hardcoded the path attribute on the forms tag under authentication in the web.config then the systems would work fine and not effect each other. So I set about trying to set the path dynamically but I was doing it all over and it wasn’t working.

My stupidity lead me into trying to set the Path on each cookie. That, however,  wasn’t the problem. A Colleague of mine found the solution this morning though. After we log the user into the system we fire the following code:

FormsAuthentication.RedirectFromLoginPage(authToken, true);

However the RedirectFromLoginPage does take a 3rd parameter which will be the path that I’ve been trying to set. So now the code reads:

string applicationBasePath = Request.ApplicationPath;
FormsAuthentication.RedirectFromLoginPage(authToken, chkRememberMe.Checked, applicationBasePath);

So once that was done everything worked like a charm!