微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用MVC和ASP.NET身份认证网址

我想要保护我的应用程序中特定的文件夹和资源,这些文件夹和资源在我的mvc应用程序的路由之外.我希望这些资源仅对经过身份验证的用户可用(只要身份验证,该角色不是有疑问的).

最初似乎UrlAuthorizationModule将是答案.我遵循这篇文章,Understanding IIS 7.0 URL Authorization,我可以让模块工作在一个意义上,它响应web.config中的配置元素.

我当前的问题是,我认为它是基于IIS中的匿名用户颁发的规则,而不是asp.net identity中的经过身份验证的用户.

测试环境

我使用标准的html文件进行测试,而不是尝试加载一个脚本,因为这也将被加载到MVC管道之外.

>在Visual Studio 2015.

>新建认的.net 4.6.2 web项目
> MVC模板
>身份验证=个人用户帐号

> IIS 8(用于在Visual Studio之外测试)

>认证 – >匿名身份验证(已启用)

添加到web.config

<configuration>
...
<location path="Data">
  <system.webServer>
    <security>
      <authorization>
        <clear/>
        <add accesstype="Deny" users="*"/>
        <add accesstype="Allow" users="?"/>
      </authorization>
    </security>
  </system.webServer>
</location>
...
</configuration>

添加文件夹结构

/Data/Protected.html // this file just has some basic Hello World content to display so you can see if it is loaded or not.

观察结果

>使用此配置,数据路径中的所有内容始终被拒绝,用户是否经过身份验证并不重要.
>如果我在web.config中切换拒绝和允许的两行也是如此.
>如果我用Deny完全删除该行,则即使用户未通过身份验证,也始终允许访问.
>如果我添加角色并使用角色名称而不是用户属性角色也被完全忽略.

怎么办?

我失踪了什么如何使Url Authorization模块与MVC / WebAPI和ASP.NET Identity个人用户帐户配合工作,或者这是不可行的?

我也可以选择其他想法,也许答案是写一个自定义的HttpModule或HttpHandler?

旁注

为什么&细节

这些资源是javascript文件,简而言之,只有一部分脚本应该可用于未经身份验证的用户.根目录中有2个目录,一个用于应用程序的已认证部分,另一个用于应用程序的未认证部分.其原因与应用程序中的用户授权或安全性无关,是将应用程序的暴露表面区域限制为未认证的请求.

解决方法

[TL; DR;]
转到“完整根web.config”部分,查看所需的web.config设置.

在无痕模式下测试,以防止浏览器缓存问题!
并使用Ctrl F5,因为脚本和html文件被缓存.

首先拒绝访问根web.config中的所有匿名用户.

<authorization>
    <deny users="?"/>        
</authorization>

这里的web.config允许一个文件夹被公开访问.这个文件夹,在我的示例中,被称为css,它位于MVC应用程序的根目录下.对于css文件夹,我将以下授权添加到根web.config中:

<location path="css">
    <system.web>
        <authorization>          
            <allow users="*"/>
        </authorization>
    </system.web>
</location>

如果您想要更多的公用文件夹,您可以添加更多的这些位置路径.

尽管所有其他文件用户登录之前将无法访问,但是始终可以访问css文件夹及其内容.

我还将一个静态文件处理程序添加到根web.config中,这是至关重要的,因为您希望由asp.net管道为特定文件类型管理请求:

<handlers>
    <add name="HtmlScriptHandler" path="*.html" verb="*" preCondition="integratedMode" type="System.Web.staticfilehandler" />
</handlers>

完成根web.config

<system.web>
    <authentication mode="None" />
    <authorization>
        <deny users="?"/>        
    </authorization>
    <compilation debug="true" targetFramework="4.6.2" />
    <httpRuntime targetFramework="4.6.2" />
</system.web>
<location path="css">
    <system.web>
        <authorization>          
            <allow users="*"/>
        </authorization>
    </system.web>
</location>
<system.webServer>
    <modules>
        <remove name="FormsAuthentication" />           
        <remove  name="UrlAuthorization" />
        <add  name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule"  />     
    </modules>
    <handlers>
        <add name="HtmlScriptHandler" path="*.html" verb="*" preCondition="integratedMode" type="System.Web.staticfilehandler" />
    </handlers>      
</system.webServer>

认情况下,ASP.NET将仅将allow和deny规则应用于托管处理程序处理的文件.静态文件不由托管处理程序管理.

你也可以设置:(不要这样做,如果不是真的需要!)

<modules runAllManagedModulesForAllRequests="true">

使用runAllManagedModulesForAllRequests =“true”,所有HTTP模块将在每个请求上运行,而不仅仅是托管请求(例如.aspx,ashx).这意味着模块将在每个.jpg,.gif,.css,.html,.pdf,…请求上运行.

一件重要的事情
您不必将UrlAuthorizationModule添加到模块部分,因为它已经是ASP.NET管道的一部分.这意味着它只会运行于托管文件,而不是静态的!

如果现在删除然后重新添加UrlAuthorizationModule到模块部分,它将运行在前提条件“integratedMode”,而不是在“managedHandler”下面!因此,可以访问静态文件.

<remove  name="UrlAuthorization" />
<add  name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" />

如果设置前提条件进行管理:
< add name =“UrlAuthorization”type =“System.Web.Security.UrlAuthorizationModule”preCondition =“managedHandler”/&gt,则UrlAuthorizationModule不再限制对静态文件的访问. 您可以通过在登录时成功访问脚本文件夹中的脚本文件来测试此操作.按住Ctrl F5确保您获得脚本文件的新副本. Difference between ASP.NET UrlAuthorization <–> IIS URL Authorization

It is important to keep in mind that the managedHandler precondition
is on the ASP.NET UrlAuthorization module. The precondition tells you
that the URL authorization module is invoked only when the code that
handles the request is mapped to managed code,typically an .aspx or
.asmx page. IIS URL Authorization,on the other hand,applies to all
content. You can remove the managedHandler precondition from the
ASP.NET Url Authorization module. It is there to prevent a performance
penality you have to pay when every request (such as a request to
.html or .jpg pages) would have to go through managed code.

P.S .:一些web.config属性是区分大小写的!

原文地址:https://www.jb51.cc/aspnet/250282.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐