【问题标题】:How to show default page in ASP.NET form authentication Web.Apps如何在 ASP.NET 表单身份验证 Web.Apps 中显示默认页面
【发布时间】:2015-04-16 15:35:22
【问题描述】:

我想实现的目标

我将 index.html(default.html, etc) 设置为 ASP.NET Web 应用程序中最流行的默认页面,并希望在用户设置没有页面名称的 WebApps 的 url 时让它显示。

我想,如果 WebApp 的 url 是类似'http://www.somewhere.com/testApps/',当你在浏览器的地址栏中设置它时,会显示 index.html。

但是如果 web 应用程序使用表单身份验证,它就不起作用了。

环境

  • 操作系统:Windows 7 Pro 或 Windows Server 2008
  • IIS 服务器:IIS 7.5.7600
  • Web 应用程序/ASP.NET 4.0、ASP.NET 2.0

重现问题的方法

  1. 制作一个使用表单的简单 ASP.NET Web 应用程序 验证。 f.e. WebApp url 是'http://localhost/testApps/'
  2. 制作一个简单的登录表单,在以下情况下发布身份验证票 用户输入任何ID和密码,并将其设置为表单的登录表单 web.config 中的身份验证。
  3. Default.aspx 只是在文本框中显示页面名称,即 在 Page_Load 方法中指定。
  4. 制作一个在正文中静态显示其页面名称的 Index.html。
  5. 在 web.config 的最后部分设置以下条目。

  6. 到目前为止,当您设置“http://localhost/testApps/Index.html”时, 显示 Index.html。但是当你设置'http://localhost/testApps/'时, 显示登录页面而不是 Index.html


就是这样。任何意见、建议、打击将不胜感激。

谢谢。

【问题讨论】:

  • 在发布问题之前您是否进行过搜索。网上有大量的解决方案。
  • 嗨拉胡尔,谢谢您的建议。实际上我试图搜索类似案例,但我不能。在我想出我的解决方案后,我找到了显示相同解决方案的主题! stackoverflow.com/questions/3824951/… 谢谢。

标签: c# asp.net iis default form-authentication


【解决方案1】:

将此添加到 web.config 以禁用此页面中的身份验证

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

【讨论】:

  • 嗨,Cesar BA。我之前尝试过这个 web.config 设置,但效果不佳。我猜表单身份验证设置优于 IIS 默认页面设置。因此,在http请求到达默认页面之前,表单认证被踢了。我猜是这样的。
  • 当然,在你的IIS第一个列表默认页面应该是index.html
【解决方案2】:

我找到了解决方案

我已经改变了解决这个问题的方法,并修改了 global.asax 中的 AuthenticationRequest 事件处理程序。

正如我所料,当我实现 AuthenticationRequest 事件处理程序时,很明显,只要 WebApp 的 url 没有页面被调用,它就会被调用。 (例如'http://localhost/testApps/')。当然是在默认页面发生之前的同一个事件处理程序。

所以我在 global.asax 中实现了以下逻辑。


// bind custom method to authenctionrequest event handler.
public Global(): base()
{
    this.AuthenticateRequest += new EventHandler(this.MyAuthenticationRequest);
}

// the custom method
void MyAuthenticationRequest(object sender, EventArgs e)
{
    var myPage = HttpContext.Current.Request.Url.ToString();
    myPage = myPage.Replace(Request.Url.GetLeftPart(UriPartial.Path),
        String.Empty);
    if (String.IsNullOrEmpty(myPage))
    {
        if (Request.Cookies["started"] != null)
        {
            String flg = Request.Cookies["started"].Value;
            if (flg == "true")
            {
                return;
            }
        }
        Response.Cookies.Add(new HttpCookie("started", "true"));
        Response.Redirect("Index.html");
    }
}

cookie 'started' 已设置为即使 'Index.html' 重新启动 web.config 的条目并再次请求表单身份验证。


通过这种方法,在表单认证的情况下,我可以在设置'http://localhost/testApps/'时显示'Index.html'。

就是这样。谢谢。

【讨论】:

    【解决方案3】:

    另一个终极解决方案。这是更简单实用的方法。

    在 web.conf 的部分内设置以下条目

    <urlMappings enabled="true">
    <add url="~/" mappedUrl="~/Index.html" />
    </urlMappings>
    

    这两种方法都很好,但我自己推荐简单的方法。

    坦克。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-12
      • 1970-01-01
      • 1970-01-01
      • 2019-05-22
      • 1970-01-01
      • 2014-03-03
      • 2011-02-26
      相关资源
      最近更新 更多