【问题标题】:Forms Authentication and infinite redirects?表单身份验证和无限重定向?
【发布时间】:2019-01-08 20:04:00
【问题描述】:

我们在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_Loadlogin.aspx 看起来像这样。使用 Forms Authentication,访问任何 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。这是一个永无止境的循环。

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

【问题讨论】:

  • 一个可能的建议是修改Sales.html 以检查当用户包含在配置列表中时您传递的某种查询字符串参数,如果发送了该参数,则允许无需重定向登录即可访问。
  • 但我需要将服务器端代码添加到所有 html 页面。
  • 从你解释的方式来看,听起来你只需要在重定向到Sales.html 时担心这个问题,但我想你可以做某种继承,让你的所有页面都继承自它,所以你只能添加一次重定向逻辑。
  • 所有的html页面都是纯html。我没有做任何继承,因为这意味着我必须将服务器端代码添加到所有 html 页面。
  • 您不能像本文 (stackoverflow.com/questions/3628445/…) 和本文 (forums.asp.net/t/…) 中所述那样将异常放入 web.config 文件中吗?然后,您可以只授予配置中特定用户的访问权限。

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-07
    • 2019-09-10
    相关资源
    最近更新 更多