StevenMcD
StevenMcD

Reputation: 17482

Set Path dynamically in Forms Authentication

Here's the problem we facing.

In a hosted environment setup, we're hosting the same project multiple times. We currently manually specify a Path in the forms config section of our web.config. However, to smooth out our deployment process, we'd like to set the Path depending on the Virtual Directory name.

Is there a way for us to dynamically set the Path in the web.config?

Upvotes: 1

Views: 883

Answers (1)

stevemegson
stevemegson

Reputation: 12093

There's an overload of FormsAuthentication.SetAuthCookie that takes the cookie path as a parameter, so if you're handling the login process yourself then you can just pass the path of your choice.

The problem is that the standard System.Web.UI.WebControls.Login will only use the default path value. You could, however, handle the LoggedIn event to fix the path...

void FixCookie( object sender, EventArgs args )
{
    Response.Cookies[FormsAuthentication.FormsCookieName].Path = "/my-custom-path";
}

Upvotes: 1

Related Questions