【问题标题】:How to restrict static file that only authenticated user can access?如何限制只有经过身份验证的用户才能访问的静态文件?
【发布时间】:2018-09-09 09:04:02
【问题描述】:

不允许用户访问 HTML 文件,只有经过身份验证的用户才能访问 HTML 文件。我已经在 web.config 文件中添加了身份验证,但这不起作用。

 <location path="test">
    <system.web>
      <authorization>
        <allow users="?"/>
        <deny users="*"/>
      </authorization>
    </system.web>
  </location>

当我将此代码放入我的 Web.config 文件时,只有未经身份验证的用户才能访问 HTML 文件。当我通过应用程序身份验证登录时,它无法访问 HTML 文件。

此代码仅拒绝访问,不允许经过身份验证的用户访问“test”文件夹。

提前致谢!!!

【问题讨论】:

  • 你可以试试this我试过了就解决了
  • 嗨你可以试试this我试过了然后解决了我的问题。

标签: c# asp.net .net asp.net-mvc visual-studio


【解决方案1】:

嗯,我有办法使用 httphandlers。

首先在您的项目中添加一个类并实现 IHttpHandler 类。然后 将此示例代码添加到 ProcessRequest 方法中。

 if (!HttpContext.Current.User.Identity.IsAuthenticated )
                {
                    HttpContext.Current.Response.Write("//No authenticated access to static files are allowed");
                    return;
                }
var requestedFile=File.ReadAllText(HttpContext.Current.Request.PhysicalPath);
   HttpContext.Current.Response.ContentType = "text/html";
                HttpContext.Current.Response.Write(requestedFile);

当然不要忘记将此引用添加到网络配置中。

 <add verb="*" path="*.html" type="YourNamespace.YourClassName, YourNameSpace" name="JS" />

希望对你有帮助。

【讨论】:

  • 此代码适用于每个 URL 请求以及 ProcessRequest 方法的放置位置。
猜你喜欢
  • 1970-01-01
  • 2017-01-17
  • 2019-01-25
  • 1970-01-01
  • 1970-01-01
  • 2022-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多