gpt4 book ai didi

c# - 如何强制注销网站的所有用户?

转载 作者:IT老高 更新时间:2023-10-29 00:04:02 26 4
gpt4 key购买 nike

我正在使用 MySQL Connector/.NET,它的所有提供程序都带有 FormsAuthentication。

我需要所有用户在某个时刻注销。 FormsAuthentication.SignOut() 方法无法正常工作。

如何注销所有站点用户?

最佳答案

正如 Joe 建议的那样,您可以编写一个 HttpModule 来使给定 DateTime 之前存在的任何 cookie 无效。如果你把它放在配置文件中,你可以在必要时添加/删除它。例如,

Web.config:

<appSettings>
<add key="forcedLogout" value="30-Mar-2011 5:00 pm" />
</appSettings>

<httpModules>
<add name="LogoutModule" type="MyAssembly.Security.LogoutModule, MyAssembly"/>
</httpModules>

MyAssembly.dll 中的 HttpModule:

public class LogoutModule: IHttpModule
{
#region IHttpModule Members
void IHttpModule.Dispose() { }
void IHttpModule.Init(HttpApplication context)
{
context.AuthenticateRequest += new EventHandler(context_AuthenticateRequest);
}
#endregion


/// <summary>
/// Handle the authentication request and force logouts according to web.config
/// </summary>
/// <remarks>See "How To Implement IPrincipal" in MSDN</remarks>
private void context_AuthenticateRequest(object sender, EventArgs e)
{
HttpApplication a = (HttpApplication)sender;
HttpContext context = a.Context;

// Extract the forms authentication cookie
string cookieName = FormsAuthentication.FormsCookieName;
HttpCookie authCookie = context.Request.Cookies[cookieName];
DateTime? logoutTime = ConfigurationManager.AppSettings["forcedLogout"] as DateTime?;
if (authCookie != null && logoutTime != null && authCookie.Expires < logoutTime.Value)
{
// Delete the auth cookie and let them start over.
authCookie.Expires = DateTime.Now.AddDays(-1);
context.Response.Cookies.Add(authCookie);
context.Response.Redirect(FormsAuthentication.LoginUrl);
context.Response.End();
}
}
}

关于c# - 如何强制注销网站的所有用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5488547/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com