【问题标题】:Dynamically change Owin configuration values动态更改 Owin 配置值
【发布时间】:2016-09-20 04:45:29
【问题描述】:

我正在使用 Owin 管道并在 startup.auth.cs 中设置应用程序 cookie 间隔,如下所示 timeout=Convert.ToDouble(ConfigurationManager.AppSettings["SessionTimeOut"]);

           // Owin Middleware3 - Cookie Authentication Middleware

            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                LoginPath = new PathString("/Account/Login"),
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                ExpireTimeSpan = TimeSpan.FromMinutes(timeout),
                SlidingExpiration = true

                }
            });

当我在 web.config 中更改 SessionTimeout 的值时,我需要重新启动 IIS 以获取新值,因为仅第一次调用 startup.auth.cs。无论如何我可以在不重新启动 IIS 的情况下动态更改 cookie expiretimespan。我还使用 kento.authservices 进行单点登录,我在 startup.auth.cs 中配置。我还需要动态更改其中的配置值。请你帮忙。

【问题讨论】:

  • 我相信你什么都不做,也不需要像更改 web.config 时那样重新启动 IIS,这也会使应用程序池重新启动
  • 谢谢Cuong。但是 startup.auth.cs 中的一些配置值是从数据库中获取的。当应用程序在 startup.auth.cs 中启动时,该值将从数据库中获取。但是在用户登录后,这些值通过管理页面在数据库中发生更改,并且在用户注销并重新启动后,应该发生新的更改值,但这不会发生,因为 startup.auth.cs 仅在以下情况下被调用一次应用程序第一次启动。如何动态更改这些值。

标签: c# asp.net-mvc-4 cookies owin owin-middleware


【解决方案1】:

除了将新对象 CookieAuthenticationOptions 传递给 UseCookieAuthentication 之外,您还可以保留一个引用并传递该引用。这样您就可以在 ExpireTimeSpan 更改时设置它。

CookieAuthenticationOptions Co = new CookieAuthenticationOptions();

然后在值变化时在代码中设置Co.ExpireTimeSpan。

【讨论】:

  • 谢谢阿布舍克。请您提供一个链接或示例以供参考。
  • 在您的情况下,您可以进行以下更改: CookieAuthenticationOptions co = new CookieAuthenticationOptions(new PathString("/Account/Login"),DefaultAuthenticationTypes.ApplicationCookie,TimeSpan.FromMinutes(timeout),true); app.UseCookieAuthentication(co);然后,当从数据库中检索到新值时,您可以将其分配给 co.ExpiresTimeSpan :- Co.ExpiresTimeSpan = ;您现在可以通过引用 co 访问 CookieAuthenticationOptions 对象中的值 ExpiresTImeSpan
  • 谢谢阿布舍克。但我正在从另一个页面检索过期时间跨度。在该页面中( IAppbuilder )的应用程序不可用,无法分配新的 cookie 身份验证选项。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-16
  • 2013-08-10
  • 1970-01-01
相关资源
最近更新 更多