ウェブサイトのすべてのユーザーを強制的にログアウトするにはどうすればいいですか? 質問する

ウェブサイトのすべてのユーザーを強制的にログアウトするにはどうすればいいですか? 質問する

私は MySQL Connector/.NET と、FormsAuthentication を備えたすべてのプロバイダーを使用しています。

ある時点ですべてのユーザーをログアウトさせる必要があります。この方法はFormsAuthentication.SignOut()期待どおりには機能しません。

すべてのサイトユーザーをログアウトするにはどうすればいいですか?

ベストアンサー1

Joe が提案しているように、特定の DateTime より前に存在するすべての Cookie を無効にする HttpModule を書くことができます。これを設定ファイルに記述すると、必要に応じて追加/削除できます。たとえば、

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();
        }
    }
}

おすすめ記事