1

主题。我有一个 ASP.NET 2 MVC Worker Role Application,它与默认模板没有太大区别。

当尝试从 HTTP 重定向到 HTTPS 时(当我们访问由通常的 RequireSSL 属性实现保护的控制器时会发生这种情况),我们会得到带有“Bad Request”消息的空白页面。

IntelliTrace 显示:

抛出:“文件'/Views/Home/LogOnUserControl.aspx'不存在。” (System.Web.HttpException)

调用堆栈真的很短:

[External Code] 
App_Web_vfahw7gz.dll!ASP.views_shared_site_master.__Render__control1(System.Web.UI.HtmlTextWriter __w = {unknown}, System.Web.UI.Control parameterContainer = {unknown})    
[External Code] 
App_Web_bsbqxr44.dll!ASP.views_home_index_aspx.ProcessRequest(System.Web.HttpContext context = {unknown})   
[External Code] 

用户控件引用是 /Views/Shared/Site.Master 中的常用控件:

<div id="logindisplay">
    <% Html.RenderPartial("LogOnUserControl"); %>
</div> 

部分视图 LogOnUserControl.ashx 位于 Views/Shared 中(它是 ASCX,而不是 ASPX)。

当我们尝试访问需要 auth (FormsAuth) 和重定向的网站页面时,问题就出现了,因为 SSL。这些页面受 RequireSSL 属性保护(重定向 == true):

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
public sealed class RequireSslAttribute : FilterAttribute, IAuthorizationFilter
{
  public bool Redirect { get; set; }

  // Methods
  public void OnAuthorization(AuthorizationContext filterContext)
  {
    // this get's messy, when we are running custom ports
    // within the local dev fabric.
    // hence we disable code in the debug

#if !DEBUG
    if (filterContext == null)
    {
      throw new ArgumentNullException("filterContext");
    }

    if (filterContext.HttpContext.Request.IsSecureConnection)
      return;

    var canRedirect = string.Equals(filterContext.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase);

    if (canRedirect && Redirect)
    {
      var builder = new UriBuilder
      {
        Scheme = "https",
        Host = filterContext.HttpContext.Request.Url.Host,
        Path = filterContext.HttpContext.Request.RawUrl
      };
      filterContext.Result = new RedirectResult(builder.ToString());
    }
    else
    {
      throw new HttpException(0x193, "Access forbidden. The requested resource requires an SSL connection.");
    }
#endif
  }
}

显然,我们在 RELEASE 中编译了这种情况。

有谁知道,什么可能导致这个奇怪的异常以及如何摆脱它?

4

2 回答 2

0

您的“LogOnUserControl”部分视图是 ashx 文件而不是 aspx/ascx 是否有特定原因?我的印象是 MVC 只查找 .aspx 和 .ascx 文件作为约定。

于 2010-05-27T11:01:03.047 回答
0

注意:这不是正确的解决方案,但它现在有效。通过使用 RequireSsl 标记整个家庭控制器,我们强制所有访问者从一开始就使用 Https。

仍然可以通过调用类似的方法来遇到错误:

http://project.company.com/User/Login?ReturnUrl=/solution

但它不太可能通过浏览网站来获得这样的链接,所以这暂时可行。

于 2010-05-27T10:51:44.583 回答