表单身份验证和无限重定向?

时间:2019-01-08 20:04:01

标签: c# asp.net authentication iis-7 .net-4.5

我们在IIS7的虚拟目录 Payroll 下有几个HTML页面。 html页面之一称为 Sales.html 。所有这些页面都是纯HTML。

我已启用Forms Authentication并修改了web.config,以使HTML页面属于这种身份验证类型。这就是我的web.config的样子:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="Users" value="BobJ,JosephB"/>
  </appSettings>
  <system.webServer>
    <handlers>
      <add name="HTML" path="*.html" verb="GET, HEAD, POST, DEBUG"   type="System.Web.UI.PageHandlerFactory" resourceType="Unspecified" requireAccess="Script" />
      <add name="HTM" path="*.htm" verb="GET, HEAD, POST, DEBUG" type="System.Web.UI.PageHandlerFactory" resourceType="Unspecified" requireAccess="Script" />
    </handlers>
  </system.webServer>
  <system.web>
    <authentication mode="Forms">
      <forms name="appNameAuth" path="/" loginUrl="login.aspx" defaultUrl="index.html" protection="All" timeout="525600">
        <credentials passwordFormat="Clear">
          <user name="[user]" password="[password]" />
        </credentials>
      </forms>
    </authentication>
    <authorization>
      <deny users="?" />
    </authorization>
    <compilation debug="true" targetFramework="4.5">
      <buildProviders>
        <add extension=".html" type="System.Web.Compilation.PageBuildProvider" />
        <add extension=".htm" type="System.Web.Compilation.PageBuildProvider" />
      </buildProviders>
    </compilation>
    <httpRuntime targetFramework="4.5" />
    <customErrors mode="Off"/>
  </system.web>
</configuration>

因此Page_Load中的login.aspx看起来像这样。使用 Forms身份验证,访问任何 Sales.html 的任何人都将被重定向到login.aspx。 我想做的是检查page_load中的用户与web.config中的用户列表,并根据用户Response.Redirect进行比较:

protected void Page_Load(object sender, EventArgs e)
{
    string htmlPage = Convert.ToString(Request.QueryString["ReturnUrl"]);
    string user = Request.LogonUserIdentity.Name;

    string users = ConfigurationManager.AppSettings["Users"].ToString();

    string[] allUsers = users.Split(',');

    if (allUsers.ToList().Contains(user))
    {
        Response.Redirect(htmlPage);
    }
    else
    {
        Response.Redirect("InvalidUser.html");
    }

}

问题在于无限重定向:每次page_load重定向到 Sales.html 时,它将带我到 login.aspx ,然后将其转到我*再次*访问 Sales.html 。这是一个永无止境的循环。

我有什么选择?我不想创建登录页面。

1 个答案:

答案 0 :(得分:0)

我最终使用了Windows Authentication for Specific windows user group中的解决方案,并在<authorization>内添加了<system.web>

<authorization>
  <allow users="domain\bobj, domain\ralphk" />
  <deny users="*" />
</authorization>

我的web.config看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="HTML" path="*.html" verb="GET, HEAD, POST, DEBUG" type="System.Web.UI.PageHandlerFactory" resourceType="Unspecified" requireAccess="Script" />
      <add name="HTM" path="*.htm" verb="GET, HEAD, POST, DEBUG" type="System.Web.UI.PageHandlerFactory" resourceType="Unspecified" requireAccess="Script" />
    </handlers>
        <staticContent>
            <mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
        </staticContent>
  </system.webServer>
  <system.web>
    <authorization>
      <allow users="domain\bobj, domain\ralphk" />
      <deny users="*" />
    </authorization>
    <compilation targetFramework="4.5">
      <buildProviders>
        <add extension=".html" type="System.Web.Compilation.PageBuildProvider" />
        <add extension=".htm" type="System.Web.Compilation.PageBuildProvider" />
      </buildProviders>
    </compilation>
    <httpRuntime targetFramework="4.5" />
    <customErrors mode="Off" />
        <identity impersonate="false" />
  </system.web>
</configuration>