我启用了 SSL,我正在尝试自己编写RequireHttpsAttribute
以允许使用 IIS Express 轻松进行测试。当它重定向时,它重定向到端口 44301,但我不想硬编码,我想从现有配置中读取它。
public sealed class RequireHttpsAlternativePortAttribute : RequireHttpsAttribute
{
protected override void HandleNonHttpsRequest(AuthorizationContext filterContext)
{
base.HandleNonHttpsRequest(filterContext);
//alter the port in the base classes result
var baseRedirect = filterContext.Result as RedirectResult;
if (baseRedirect == null)
{
throw new Exception("No redirect was suppied from the base class");
}
var builder = new UriBuilder(baseRedirect.Url);
var targetPort = 44301; //need this from settings
if (builder.Port == targetPort) return; //already correct port
//change the port
builder.Port = targetPort;
var redirectResult = new RedirectResult(builder.ToString());
filterContext.Result = redirectResult;
}
}
44301 在 SSL URL 下的 MVC 项目属性中定义。我的问题是,这在运行时任何地方都可以访问吗?