【问题标题】:How to pass windows authentication from ".aspx" page to ".ashx" handler如何将 Windows 身份验证从“.aspx”页面传递到“.ashx”处理程序
【发布时间】:2013-11-01 18:26:35
【问题描述】:

我有一个需要 Windows 身份验证的页面“Download.aspx”。当用户登录时,他们会看到一个他们可以下载的文件的链接列表。这些链接实际上指向一个“ZipHandler.ashx”处理程序,该处理程序根据传递的参数处理请求。

我的问题是,当经过身份验证的用户访问该处理程序时,IHttpHandlerProcessRequest 方法中的 falsefalse

我需要检查它们是否在 IHttpHandler 中进行了身份验证,因为未经身份验证的用户也可以访问具有不同结果的处理程序。我已经在“Download.aspx”页面中测试了HttpContext.Current.User.Identity.IsAuthenticated 的值,该值是true,所以我不明白为什么ashx 处理程序不是这种情况。我尝试将 IReadOnlySessionState 和 IRequiresSessionState 接口添加到我的处理程序,但我仍然遇到同样的问题。

【问题讨论】:

标签: c# asp.net httphandler ntlm


【解决方案1】:

问题在于 .ashx 处理程序允许匿名访问,但如果处理程序允许匿名访问,Windows 身份验证甚至不会传递 WindowsIdentity。我解决这个问题的方法是在 web.config 中创建 2 个指向同一个处理程序类的处理程序条目:

<add name="AnonymousHandler" verb="GET" path="*/AnonymousHandler.ashx"
     type="MyLibrary.MyHandler, MyHandler, Version=1.0.0.0, Culture=neutral, 
     PublicKeyToken=12e530ccad45314d"/>

<add name="AuthenticatedHandler" verb="GET" path="*/AuthenticatedHandler.ashx" 
     type="MyLibrary.MyHandler, MyHandler, Version=1.0.0.0, Culture=neutral,
     PublicKeyToken=12e530ccad45314d"/>

然后我拒绝匿名访问经过身份验证的版本:

<location path="AuthenticatedHandler.ashx">
  <system.web>
    <authorization>
      <deny users="?"/>
    </authorization>
  </system.web>
</location>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-02
    • 2014-01-22
    • 2015-08-14
    • 2013-04-03
    相关资源
    最近更新 更多