【问题标题】:Secure sub site with static html pages with forms authentication of parent site使用带有父站点表单身份验证的静态 html 页面保护子站点
【发布时间】:2018-01-31 17:20:23
【问题描述】:

我在 IIS 8 中有一个 ASP .NET MVC 站点,该站点使用表单身份验证进行保护。在此站点中,我有子站点(这是通过 IIS 8 中的“添加应用程序”选项创建的应用程序)。该子站点由静态 html 页面组成。所以例如我的主站点 URL 是 www.site.com。子站点网址是 www.site.com/mysite

我需要配置 mysite,这样如果用户登录到 www.site.com,则只能访问 mysite。尝试直接访问 mysite 应该重定向到 www.site.com 登录页面。

我在这方面搜索了很多,并在 SO 中找到了一些文章。

尝试 1: How do I protect static files with ASP.NET form authentication on IIS 7.5?

基于这个 SO question,我尝试更改父站点的 web.config。我在这里提一下

<system.webServer>
    <modules>
      <add  name="FormsAuthenticationModule"  type="System.Web.Security.FormsAuthenticationModule" />
      <remove  name="UrlAuthorization" />
      <add  name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule"  />
      <remove  name="DefaultAuthentication" />
      <add  name="DefaultAuthentication"  type="System.Web.Security.DefaultAuthenticationModule" />
    </modules>
 </system.webServer>

<system.web>
    <authorization>
        <deny users="?" />
    </authorization>
     <authentication mode="Forms">
    <forms defaultUrl="/Home/Index" loginUrl="/" protection="All" timeout="90">
    </forms>
  </authentication>
</system.web> 

但是,通过这些设置,当我尝试打开 www.site.com/mysite 时,它​​会将我重定向到带有 www.site.com?returnUrl=mysite 的登录页面。但是登录后它再次将我重定向到登录页面

尝试 2: How to do Forms Authentication on purely HTML pages using ASP.NET?

根据这个 SO question,我尝试通过在子站点 (mysite) 的 web.config 中添加以下内容,使子站点 www.site.com/mysite 由 ASP .NET 处理。然而没有快乐

<compilation>
    <buildProviders>
        <add extension=".html" type="System.Web.Compilation.PageBuildProvider" />
        <add extension=".htm" type="System.Web.Compilation.PageBuildProvider" />
    </buildProviders>
</compilation>

尝试 3:

我尝试直接在子站点 (www.site.com/mysite) 的 web.config 文件中添加表单身份验证和授权设置

<system.web>
        <authorization>
            <deny users="?" />
        </authorization>
         <authentication mode="Forms">
        <forms defaultUrl="/Home/Index" loginUrl="/" protection="All" timeout="90" domain="site.com">
        </forms>
      </authentication>
    </system.web> 

这里要注意的重要一点是

域名

属性添加到表单标签。还是不开心!

现在我想知道我正在尝试的东西是否可能?或者有没有我可能不知道的替代解决方案?

【问题讨论】:

    标签: asp.net asp.net-mvc authentication authorization iis-8


    【解决方案1】:

    解决方法如下:

    第 1 步

    使用要由 ASP .NET 处理的静态 html 页面制作子站点

    <compilation>
        <buildProviders>
            <add extension=".html" type="System.Web.Compilation.PageBuildProvider" />
            <add extension=".htm" type="System.Web.Compilation.PageBuildProvider" />
        </buildProviders>
    </compilation>
    

    第 2 步:

    在父(主)站点和子(子)站点之间共享表单身份验证密钥,即在父站点 web.config 和子站点 web.config 中添加以下内容

    <system.web>
    <machineKey decryptionKey="Yourdecryptionkey" validationKey="Your validation 
        key" />
    </system.web>
    
    • 为了生成这些密钥,在 IIS 中选择您的站点。
    • 双击右侧窗格中的 MachineKey 选项。
    • 这里取消选中“运行时自动生成”选项和 “为每个应用程序生成唯一密钥”用于验证密钥和 解密密钥。这将填充验证密钥和 解密密钥盒。现在您可以复制这些密钥并在 您的子网站

    【讨论】:

    • 我遇到了类似的问题,您的解决方案还不够。我不需要第 1 步,但我需要第 2 步,并在 machineKey 中添加了 compatibleMode,因为我为我的子站点使用了不同的 .Net 版本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多