使用静态html页面保护子站点,并使用父站点

时间:2018-01-31 17:20:24

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

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

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

我在Google上搜索了很多内容并在SO中发现了一些文章。

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

基于这个问题,我尝试更改父网站的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?

根据这个问题,我试图让子网站www.site.com/mysite由ASP .NET处理,在下面的子网站(mysite)的web.config中添加。然而,没有快乐

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

尝试3:

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

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

这里需要注意的重要事项是

  

属性已添加到Forms标记中。仍然没有快乐!

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

1 个答案:

答案 0 :(得分:0)

解决方案如下:

第1步

使用ASP .NET处理具有静态html页面的子网站

    int playerAGamesWon = 0;
    int playerBGamesWon = 0;
    int playerSkill = 50;
    int numberOfSets = 10;
    int numberOfGames = 6;
    int numberOfPoints = 4;


    void SimulateTennis()
    {
        txtData.Text = "Simulating game of tennis"+Environment.NewLine;
        Random rand = new Random();

        for (int i = 0; i < numberOfSets; i++)
        {
            while (true)
            {
                calculatePlayerPoints(rand);
                if (playerAGamesWon >= numberOfGames || playerBGamesWon >= numberOfGames)
                {
                    if (playerAGamesWon - playerBGamesWon >= 2)
                    {
                        playerASetWon++;
                        txtData.Text += "Player A won the Set with " + playerAGamesWon + " games to " + playerBGamesWon + Environment.NewLine + Environment.NewLine;
                        playerAGamesWon = 0;
                        playerBGamesWon = 0;
                        break;
                    }
                    else if (playerBGamesWon - playerAGamesWon >= 2)
                    {
                        playerBSetWon++;
                        txtData.Text += "Player B won the Set with "+ playerBGamesWon + " games to " + playerAGamesWon + Environment.NewLine + Environment.NewLine;
                        playerAGamesWon = 0;
                        playerBGamesWon = 0;
                        break;
                    }
                    else
                    {
                        continue;
                    }
                }
            }
        }
    }
    void calculatePlayerPoints(Random rand)
    {
        int apoints = 0;
        int bpoints = 0;
        while (true)
        {
            int prob = rand.Next(100);
            if (prob <= playerSkill)
            {
                apoints++;
            }
            else
            {
                bpoints++;
            }
            if (apoints >= numberOfPoints)
            {
                if (apoints - bpoints >= 2)
                {
                    playerAGamesWon++;
                    updateScoreText(apoints, bpoints);
                    break;
                }
            }
            //else if statement of (bpoints >= numberOfPoints) was unreachable when if statement of (apoints >= numberOfPoints) was true
            //I should use if statement
            //else if (bpoints >= numberOfPoints)
            if (bpoints >= numberOfPoints)
            {
                if (bpoints - apoints >= 2)
                {
                    playerBGamesWon++;
                    updateScoreText(apoints, bpoints);
                    break;
                }
            }
        }
    }

第2步:

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

<compilation>
    <buildProviders>
        <add extension=".html" type="System.Web.Compilation.PageBuildProvider" />
        <add extension=".htm" type="System.Web.Compilation.PageBuildProvider" />
    </buildProviders>
</compilation>
  • 要生成这些密钥,请在IIS中选择您的站点。
  • 双击右侧窗格中的MachineKey选项。
  • 这里取消选中选项&#34;在运行时自动生成&#34;和 &#34;为每个应用程序生成唯一密钥&#34;用于验证密钥和 解密密钥。这将填充验证密钥和 解密密钥框。现在您可以复制这些密钥并将其用于 你的孩子网站