Context.User.Identity.IsAuthenticated 始终经过身份验证吗?

发布于 2024-10-20 10:42:57 字数 1791 浏览 2 评论 0原文

我正在尝试创建一个 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>

I am trying to create a httphandler which will intercept a sample pdf file which we have in our website. The httphandler works fine from within my development machine and even my locally published website that if I just try to connect to the test url:
https://test.com/admin/_/sample_reports/sample.pdf I will get sent to the invalid access page.

So pushing it to our IIS6 machine when I try to go to the URL it serves up the PDF document. context.User.Identity.IsAuthenticated is always showing as true.

I'm using forms authentication. below is the code I am using as the handler.

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");
    }}

in web.config I have the following for form authentication:

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一指流沙 2024-10-27 10:42:57

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

对于 Forms 身份验证,
表单身份验证模块使用
加密的身份验证票
包含在身份验证 cookie 中
对用户进行身份验证。一旦有了
这样做,它取代了
GenericIdentity
Context.User.Identity 带有
返回的 FormsIdentity 对象
true 来自其 IsAuthenticated
属性。

所以,你的 auth cookie 还活着;它可能是由调用其中之一引起的 FormsAuthentication 方法,如 RedirectFromLoginPage< /a> 或 SetAuthCookie 正在设置身份验证 cookie;或者只是被遗忘的cookie。

另外,最好使用 HttpRequest.IsAuthenticated 而不是您的示例中的 Context.User.Identity.IsAuthenticated 。它检查 HttpContext.UserHttpContext.User.Identity 是否不 nullHttpContext.User.Identity.IsAuthenticated > 属性设置为 true。在您的情况下,例如,当HttpContext.Usernull时,您的代码将抛出NullReferenceException

Context.User.Identity.IsAuthenticated property is set to true when authentication cookie is still set and is still valid (not expired) for forms authentication.

In the case of Forms authentication,
the forms authentication module uses
the encrypted authentication ticket
contained in the authentication cookie
to authenticate the user. Once it has
done this, it replaces the
GenericIdentity in
Context.User.Identity with a
FormsIdentity object that returns
true from its IsAuthenticated
property.

So, your auth cookie is still alive; it may be caused by calling one of those FormsAuthentication methods like RedirectFromLoginPage or SetAuthCookie which are setting the auth cookie; or just by forgotten cookie.

Also it would be better to use HttpRequest.IsAuthenticated instead of Context.User.Identity.IsAuthenticated for your example. It checks whether HttpContext.User and HttpContext.User.Identity is not null and HttpContext.User.Identity.IsAuthenticated property is set to true. In your case when e.g. HttpContext.User is null your code will throw NullReferenceException.

柒七 2024-10-27 10:42:57

你确定吗

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

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

Are you sure that

So pushing it to our IIS6 machine when I try to go to the URL it serves up the PDF document. context.User.Identity.IsAuthenticated is always showing as true.

This .PDF request may have already been processed by IIS 6 static file handler instead of your HTTP handler on IIS 6.

一袭水袖舞倾城 2024-10-27 10:42:57

您需要使用 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>

You need to use ProcessRequest

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

}

EDIT: Might be IIS that is the culprit then, do you have the following set? In IIS, anonymous access is enabled for all applications that use forms authentication

<system.web>
  <authorization>
    <deny users="?" />
  </authorization>
</system.web>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文