C# 在ASP.NET应用程序中拦截ajax调用,并在特定条件下注销用户

C# 在ASP.NET应用程序中拦截ajax调用,并在特定条件下注销用户,c#,asp.net,ajax,asp.net-mvc,C#,Asp.net,Ajax,Asp.net Mvc,我有一个使用标识和实体的ASP.NET MVC应用程序。 管理员可以启用/禁用用户。我想要实现的是,如果用户已经连接到应用程序,并且管理员禁用了他/她(通过在该用户记录中设置一个标志(disabled=true),那么该用户应该注销。 我不希望管理员自己记录用户,而是希望在下一个ajax调用中在用户端完成。 我已尝试使用global.asax.cs文件中提供的事件(protectedvoid Application\u AuthenticateRequest(object sender,Even

我有一个使用标识和实体的ASP.NET MVC应用程序。 管理员可以启用/禁用用户。我想要实现的是,如果用户已经连接到应用程序,并且管理员禁用了他/她(通过在该用户记录中设置一个标志(disabled=true),那么该用户应该注销。 我不希望管理员自己记录用户,而是希望在下一个ajax调用中在用户端完成。 我已尝试使用global.asax.cs文件中提供的事件(
protectedvoid Application\u AuthenticateRequest(object sender,EventArgs e)
),但在控制器上调用注销:

 Session.Abandon();
 Session.RemoveAll();
 AuthenticationManager.SignOut();
导致异常,表示会话为空


有谁能帮我一下吗?

如果您使用cookie身份验证,您只需使他们的cookie无效,然后就可以使用它了:

public void ConfigureAuth(IAppBuilder app)
{
    // important to register UserManager creation delegate. Won't work without it
    app.CreatePerOwinContext(UserManager.Create);

    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        Provider = new CookieAuthenticationProvider
        {
            OnValidateIdentity = SecurityStampValidator
                .OnValidateIdentity<UserManager, ApplicationUser, int>(validateInterval: TimeSpan.FromMinutes(10), regenerateIdentityCallback: (manager, user) => user.GenerateUserIdentityAsync(manager))
        },
        // other configurations
    });

    // other stuff
}
public void ConfigureAuth(IAppBuilder应用程序)
{
//注册UserManager创建委托很重要。没有它将无法工作
app.CreatePerOwinContext(UserManager.Create);
app.UseCookieAuthentication(新的CookieAuthenticationOptions
{
Provider=新CookieAuthenticationProvider
{
OnValidateIdentity=SecurityStampValidator
.OnValidateIdentity(validateInterval:TimeSpan.FromMinutes(10),regenerateIdentityCallback:(管理者,用户)=>user.GenerateUserIdentityAsync(管理者))
},
//其他配置
});
//其他东西
}

感谢Hill指导我使用此解决方案。我已使用安全戳更新来注销其他用户。