首页 > 解决方案 > 解决asp.net webforms中的cookie重放攻击

问题描述

按照StackOverflow 上的这个答案,微软建议执行以下操作:

  1. 清除 cookie。
  2. 将 Response.Status 属性设置为 401。
  3. 调用 Response.End 方法,该方法将隐式重定向到登录页面。

我在我的文件中提出了这行代码global.asax,(我在我的用户表中添加了一个额外的字段,名为isLogin我在登录和注销页面中设置为 true 和 false ...)

void Application_PostAuthenticateRequest(object sender, EventArgs e)
        {
            IPrincipal p = HttpContext.Current.User;

            if (p.Identity.IsAuthenticated)
            {
                var userName = p.Identity.Name;
                HttpCookie authenticationCookie = Request.Cookies[FormsAuthentication.FormsCookieName];

                if (SecurityUtilities.CheckUserLoggedIn(userName) == false)
                {

                    FormsAuthentication.SignOut();
                    Response.StatusCode = 401;
                    Response.End();
                }              
            }
        } 

我想知道我做这一切是否正确?这段代码呢?

if (SecurityUtilities.CheckUserLoggedIn(userName) == false)
{  

 FormsAuthentication.SignOut();
    Response.Redirect(FormsAuthentication.LoginUrl);
} 

标签: c#asp.netsecuritywebformsforms-authentication

解决方案


推荐阅读