如何在会话结束后从表单身份验证注销

时间:2014-01-30 12:27:33

标签: asp.net-mvc forms-authentication session-timeout sessionend

我的MVC应用程序使用FormsAuthentication登录。

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Login(FiNext.Models.User user)
    {
        try
        {
            using (HttpClient httpClient = new HttpClient())
            {
                var task = httpClient.PostAsJsonAsync<FiNext.Models.User>(String.Concat(ServiceUri.ApiUrl,"/Test/Validate"), user).Result;

                FormsAuthentication.SetAuthCookie(user.Name, false);
                SessionHelpers.UserId = user.Id;
                return RedirectToAction("Create");

            }
        }

它的会话时间超过1分钟(在web.config中),一旦调用会话超时,我将在Global.asax中的session_end事件中清除会话。

    protected void Session_End(object sender, EventArgs e)
    {
        Session.Clear();
        Session.Abandon();
    }

现在,当我使用页面上的正常注销按钮退出时,页面会被注销。

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult LogOff()
    {
        try
        {
            FormsAuthentication.SignOut();

            return RedirectToAction("Home", "User");
        }
        catch (Exception ex)
        {                
            throw ex;
        }
    }

现在我点击了这个应用程序的任何网址(说“http://abcd.com/User/UserList”),它被重定向到登录页面,因为我们已经注销并重定向到主页。 这是所需的功能,并且工作正常。

但问题是当会话超时且session_end事件被触发时。现在当我点击此应用程序的任何网址(比如“http://abcd.com/User/UserList”)时,我能够获取数据(不应该发生)。

因此,当触发session_end时,如何从表单身份验证中注销。 我在Global.asax中的session_end事件中尝试了这个:

    protected void Session_End(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
        Session.Clear();
        Session.Abandon();
    }

但它给出了“对象引用未设置为对象的实例”。异常。

1 个答案:

答案 0 :(得分:0)

也许我错过了一些东西,但这听起来像授权问题,而不是会话问题。

您的UserList操作是否以某种方式受到保护?

[Authorize]
public ActionResult UserList()
{
    return View();
}

http://msdn.microsoft.com/en-us/library/ff398049(v=vs.100).aspx

相关问题