【问题标题】:Context.User.Identity.IsAuthenticated always authenticated?Context.User.Identity.IsAuthenticated 总是经过身份验证?
【发布时间】:2011-03-03 17:25:54
【问题描述】:

我正在尝试创建一个 httphandler,它将拦截我们网站中的示例 pdf 文件。 httphandler 在我的开发机器甚至我本地发布的网站上都可以正常工作,如果我只是尝试连接到测试 url: https://test.com/admin/_/sample_reports/sample.pdf我将被发送到无效访问页面。

所以当我尝试访问它提供 PDF 文档的 URL 时,将它推送到我们的 IIS6 机器。 context.User.Identity.IsAuthenticated 始终显示为 true。

我正在使用表单身份验证。下面是我用作处理程序的代码。

public void ProcessRequest(HttpContext context)
{
    if (context.User.Identity.IsAuthenticated)
    {
        string SampleURL = context.Request.AppRelativeCurrentExecutionFilePath;

        context.Response.Buffer = true;
        context.Response.Clear();
        using (FileStream fs = new FileStream(HttpContext.Current.Server.MapPath(SampleURL),FileMode.Open))
        {
            int length = (int)fs.Length;
            byte[] buffer;

            using (BinaryReader br = new BinaryReader(fs))
            {
                buffer = br.ReadBytes(length);
            }

            context.Response.Clear();
            context.Response.Buffer = true;
            context.Response.ContentType = "application/pdf";
            context.Response.BinaryWrite(buffer);
            context.Response.End();
        }
    }
    else
    {
        context.Response.Redirect(
           "~/Error/invalid_access.aspx");
    }}

在 web.config 我有以下表单身份验证:

<authentication mode="Forms">
  <forms name="Sample.Web" loginUrl="~/Security/" defaultUrl="~/default.aspx" protection="All" timeout="60" path="/" requireSSL="false" slidingExpiration="true" enableCrossAppRedirects="false" cookieless="UseDeviceProfile" domain="">
  </forms>
</authentication>
<authorization>
  <deny users="?"/>
</authorization>

【问题讨论】:

    标签: asp.net iis forms-authentication


    【解决方案1】:

    当身份验证 cookie 仍然设置并且对于表单身份验证仍然有效(未过期)时,Context.User.Identity.IsAuthenticated 属性设置为 true

    在表单身份验证的情况下, 表单身份验证模块使用 加密的身份验证票 包含在身份验证 cookie 中 对用户进行身份验证。一旦有了 这样做,它取代了 GenericIdentityContext.User.IdentityFormsIdentity 返回的对象 true 来自其IsAuthenticated 属性。

    所以,您的身份验证 cookie 仍然存在;这可能是由于调用了FormsAuthentication 方法之一,例如RedirectFromLoginPageSetAuthCookie,它们正在设置身份验证cookie;或者只是通过忘记的 cookie。

    对于您的示例,最好使用HttpRequest.IsAuthenticated 而不是Context.User.Identity.IsAuthenticated。它检查HttpContext.UserHttpContext.User.Identity 是否不是nullHttpContext.User.Identity.IsAuthenticated 属性是否设置为true。在你的情况下,例如HttpContext.Usernull 你的代码会抛出 NullReferenceException

    【讨论】:

      【解决方案2】:

      你确定吗

      所以当我尝试访问它提供 PDF 文档的 URL 时,将它推送到我们的 IIS6 机器。 context.User.Identity.IsAuthenticated 始终显示为 true。

      此 .PDF 请求可能已由 IIS 6 静态文件处理程序而不是 IIS 6 上的 HTTP 处理程序处理。

      【讨论】:

      • 我会检查这个,但我不相信它是
      【解决方案3】:

      你需要使用ProcessRequest

      public void ProcessRequest(HttpContext context)
      {
          if (!context.User.Identity.IsAuthenticated)
          {
               context.Response.Redirect(
                 "~/Error/invalid_access.aspx");
          }
      
      }
      

      编辑:那可能是罪魁祸首IIS,你有以下设置吗?在 IIS 中,所有使用表单身份验证的应用程序启用匿名访问

      <system.web>
        <authorization>
          <deny users="?" />
        </authorization>
      </system.web>
      

      【讨论】:

      • 当我发布代码时,ProcessRequest 被截断,更正了帖子。
      • 查看原始帖子的更新,但是启用匿名并拒绝用户 =“?”
      猜你喜欢
      • 2018-01-05
      • 1970-01-01
      • 2018-05-21
      • 1970-01-01
      • 1970-01-01
      • 2019-09-25
      • 1970-01-01
      • 1970-01-01
      • 2017-09-01
      相关资源
      最近更新 更多