我们希望根据应用程序实例更改 FormsCookiePath 的 FormsCookieName。我们有一个应用程序,在 1 个服务器/域名上有多个实例。因此,我们只能同时在 1 个应用程序中工作,因为 cookie 会互相覆盖。顺便说一句,会话也是如此。
有没有办法动态地(例如在 Global.asax Application_Start 中)更改此名称?这很有用,因为我们在每个应用程序中保留一个许可证名称,可以用作 CookieName 的基础。
我们已经使用 Web.config 和额外文件来覆盖外部文件中的 Web.config 值:<appSettings file="Web.AppSettings.Config">
但这需要手动操作,这些操作可能会被忘记并且是多余的,因为可以从数据库中检索设置。
Thanks.
我有类似的情况,我做了以下事情。在 Application_Start 中,我检查了我的 cookie 名称是否需要更改。在对所有应用程序进行新部署(其中所有应用程序都具有相同的 web.config)后,就会发生这种情况。
protected void Application_Start(object sender, EventArgs e)
{
// determine unique cookie name per application
string cookieName = ...
// Get the web.config forms settings
Configuration c = WebConfigurationManager.OpenWebConfiguration("~");
AuthenticationSection auth = c.GetSection("system.web/authentication")
as AuthenticationSection;
// See if we have mismatch in web.config or in Forms cookiename
if (auth != null && auth.Forms != null &&
(auth.Forms.Name != cookieName
|| FormsAuthentication.FormsCookieName != cookieName
)
)
{
// Assign value in web.config for future restarts
auth.Forms.Name = cookieName;
// would be nice if this restarted the app, but it doesn't appear to
c.Save();
// This seems to restart the app
System.Web.HttpRuntime.UnloadAppDomain();
}
...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)