【问题标题】:How to change FormsCookieName at runtime in ASP.NET如何在 ASP.NET 运行时更改 FormsCookieName
【发布时间】:2016-05-26 21:04:13
【问题描述】:

我们希望 FormsCookiePath 的 FormsCookieName 更改每个应用程序实例。我们有一个应用程序,它在 1 个服务器/域名上有多个实例。因此,我们只能同时在 1 个应用程序中工作,因为 cookie 会相互覆盖。顺便说一句,Sessions 也一样。

有没有办法动态地更改这个名称,例如在 Global.asax Application_Start 中?这将很有用,因为我们在每个应用程序中都保留了一个许可证名称,该名称可用作 CookieName 的基础。

我们已经使用 Web.config 和额外文件来覆盖外部文件中的 Web.config 值,使用:<appSettings file="Web.AppSettings.Config">

但这需要手动操作,这些操作可能会被遗忘并且是多余的,因为可以从数据库中检索设置。

谢谢。

【问题讨论】:

  • 这是一个很好的答案,我冒昧地提出了解决方案here

标签: asp.net cookies forms-authentication


【解决方案1】:

我有类似的情况,我做了以下。在 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();
  }
  ...
}

在应用程序启动时修改 web.config,然后重新启动 Web 应用程序。下次启动 Web 应用程序时,cookie 名称会同步并跳过重置代码。

【讨论】:

    【解决方案2】:

    这几天我一直在与 Cookie 作斗争。这是一次很棒的学习经历。

    所以想分享一下我发现和发现的可能方式:有几个HACKs可以修改Forms Authentication Cookie name:

    1. 您可以在 Global.asax 的 Application_Start 事件中的 Web.Config 文件的 Authenticaiton 部分下自动修改 cookie 名称。感谢 Ron 分享this。但我不能保证其身份将用于运行应用程序域的用户是否有足够的权限来修改磁盘上的文件。因此我需要一个即兴的解决方案,所以我设计了以下方法。

    2. 感谢 ILSpy 让我看到 FormsAuthentication 类的内部,非常感谢 Reflection 让我修改类的私有字段。我使用以下代码在运行时使用以下一小段代码修改 cookie 名称,这就像一个魅力!


        protected void Application_Start(Object sender, EventArgs e)
        {
            // This will enforce that FormsAuthentication class is loaded from configuration settings for the application.
            FormsAuthentication.Initialize();
    
            // The new cookie name whatever you need can go here, I needed some value from my application setting to be prefixed so I used it.
            string newCookieName = string.Format("{0}.ASPXAUTH", ConfigurationManager.AppSettings["SomeSettingThatIsUniquetoSite"]);
    
            // Modifying underlying baking field that points to FormsAuthentication.FormsCookieName         
            Type type = typeof(FormsAuthentication);
            System.Reflection.FieldInfo field = type.GetField("_FormsName", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
            field.SetValue(null, newCookieName);
        }
    

    由于这是我在这个论坛上的第一个答案,所以请求建议,漏洞。

    【讨论】:

      【解决方案3】:

      根据MSDN,存储cookie名称的FormsAuthentication.FormsCookieName属性是只读属性。该属性必须从 web.config 中读取。

      每个实例在 web.config 中都需要一个单独的名称。我建议在您现有的变更管理系统中包含身份验证 cookie 的名称。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-07
        • 1970-01-01
        • 1970-01-01
        • 2012-01-12
        • 2010-11-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多