C# 以编程方式注销ASP.NET用户

C# 以编程方式注销ASP.NET用户,c#,asp.net,login,C#,Asp.net,Login,我的应用程序允许管理员挂起/取消挂起用户帐户。我使用以下代码执行此操作: MembershipUser user = Membership.GetUser(Guid.Parse(userId)); user.IsApproved = false; Membership.UpdateUser(user); 上述方法可以很好地挂起用户,但不会撤消他们的会话。因此,只要会话cookie保持不变,挂起的用户就可以继续访问应用程序。在某个公共页面上进行任何修复,请检查帐户是否有效,如果帐户已被吊销,请调

我的应用程序允许管理员挂起/取消挂起用户帐户。我使用以下代码执行此操作:

MembershipUser user = Membership.GetUser(Guid.Parse(userId));
user.IsApproved = false;
Membership.UpdateUser(user);

上述方法可以很好地挂起用户,但不会撤消他们的会话。因此,只要会话cookie保持不变,挂起的用户就可以继续访问应用程序。在某个公共页面上进行任何修复,请检查帐户是否有效,如果帐户已被吊销,请调用
会话.放弃()

编辑(刚刚注意到它仍然处于打开状态。)

我知道这是可行的,因为我做到了

在母版页上,检查帐户状态。这意味着在每次导航中,您都有机会将其注销

(最终)编辑


不要将其视为“我正在终止他们的会话”,而应将其视为“他们的会话自行终止。”

如果使用表单身份验证:

FormsAuthentication.SignOut();

无法从会话“外部”放弃会话。您必须在每次页面加载时检查数据库,如果该帐户已被禁用,则必须注销。您也可以使用HttpModule来实现这一点,这将使事情变得更干净

例如:

public class UserCheckModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.PreRequestHandlerExecute += new EventHandler(OnPreRequestHandlerExecute);
    }

    public void Dispose() {}

    private void OnPreRequestHandlerExecute(object sender, EventArgs e)
    {
        // Get the user (though the method below is probably incorrect)
        // The basic idea is to get the user record using a user key
        // stored in the session (such as the user id).
        MembershipUser user = Membership.GetUser(Guid.Parse(HttpContext.Current.Session["guid"]));

        // Ensure user is valid
        if (!user.IsApproved)
        {
            HttpContext.Current.Session.Abandon();
            FormsAuthentication.SignOut();
            HttpContext.Current.Response.Redirect("~/Login.aspx?AccountDisabled");
        }
    }
}

这不是一个完整的示例,需要调整使用会话中存储的密钥检索用户的方法,但这应该可以帮助您开始。这将涉及在每次页面加载时进行额外的数据库检查,以检查用户帐户是否仍处于活动状态,但没有其他方法检查此信息。

当您注销用户时,最好覆盖
表单身份验证票证

HttpContext context = HttpContext.Current;

//overwrite the authentication cookie
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, context.User.Identity.Name, DateTime.Now, DateTime.Now.AddDays(-1), false, Guid.NewGuid().ToString());
string encrypted_ticket = FormsAuthentication.Encrypt(ticket);

HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted_ticket);
cookie.Expires = ticket.Expiration;
context.Response.Cookies.Add(cookie);

//clear all the sessions
context.Session.Abandon();

//sign out and go to the login page
FormsAuthentication.SignOut();
FormsAuthentication.RedirectToLoginPage();

这不只是针对当前用户的会话吗?我想放弃另一个用户的会话。。。类似于Session(user).About。@Testing123 egrunin意味着每个用户都要检查自己的帐户是否无效,如果无效,则应用程序将删除他们的cookie.Downvote,因为您需要FormsAuthentication.SignOut();回复:
FormsAuthentication.SignOut()
:正如@David Burton在另一条评论中指出的那样,“他们想结束另一个会话,而不是当前用户的会话,所以这是不合适的”,在
MembershipUser=Membership.GetUser(Guid.Parse(HttpContext.current.session[“Guid”])行
,您的意思是我们应该用我们用来获取用户的任何方法替换这一行吗?想打电话到数据库吗?@guanome没错,是的。上面的代码示例假定您正在会话中存储用户ID。这行代码的目的是使用会话中存储的值从数据库中检索用户(如其ID),以便您可以检查其状态。他们希望结束另一个会话,而不是当前用户的会话,因此这不是适当的请求。GetOwinContext().Authentication.SignOut();