gpt4 book ai didi

asp.net-mvc - 从 HTTP 重定向到 HTTPS 时,Windows Azure ASP.NET MVC 角色行为异常

转载 作者:行者123 更新时间:2023-12-03 04:40:07 26 4
gpt4 key购买 nike

主题。我有一个 ASP.NET 2 MVC 辅助角色应用程序,它与默认模板没有太大区别。

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

IntelliTrace 显示如下:

Thrown: "The file '/Views/Home/LogOnUserControl.aspx' does not exist." (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>

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

当我们尝试访问由于 SSL 而需要身份验证 (FormsAuth) 和重定向的网站页面时,就会出现问题。这些页面由 RequireSSL 属性保护(Redirect == 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 中进行编译。

有人知道什么可能导致这个奇怪的异常以及如何消除它吗?

最佳答案

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

仍然有可能通过调用类似以下内容来遇到错误:

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

但是不太可能通过浏览网站来到达这样的链接,因此暂时可以使用。

关于asp.net-mvc - 从 HTTP 重定向到 HTTPS 时,Windows Azure ASP.NET MVC 角色行为异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2920219/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com