asp.net-mvc – ASP.Net身份登录重定向强制协议(Https)
|
希望我只是遗漏了一些非常简单/明显的东西 – 为什么,更重要的是,在重定向到登录期间如何维护(或强制)协议? 为了显示: >原始协议是https 我试过的东西: >可以使用RequireHttps属性,但是: >似乎“奇怪”,需要2次重定向才能获得“那里” >我已经设置了IIS URL重写(也称为整个站点的https规范规则),并且似乎“忽略”(也)(规则不检查“https”,否则它会遭受相同的重定向循环). 感谢您的建议或指点…… 更新 至于“为什么”?
进一步的修补使我进入上面,如此(localhost – 我的本地开发框,而不是服务器)请求序列(上述问题在生产负载平衡环境中显示,其中SSL处理是“堆栈” – 例如ARR): >事实上协议得到维护 那么问题是关于如何解决这个问题的指导? 更新2 非常感谢理查德在试图解决这个问题时改变了我的“方向”.我原本想找到一种方法: > set / tell OWIN / Identity使用安全URL(显式)并“覆盖”它评估LoginPath的方式.处理cookie的安全(唯一)选项以某种方式引导我(如果我只能在HTTPS中明确说出cookie,那么它给我的印象是能够为LoginPath.one方式或其他方式这样做) 最后,理查德的回答把我带到了URL重写(虽然仍然没有在LB方面,因为这超出了我的控制范围).我目前正在处理(基于我的环境): <rule name="Redirect to HTTPS" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_CLUSTER_HTTPS}" pattern="^on$" negate="true" />
<add input="{HTTP_CLUSTER_HTTPS}" pattern=".+" negate="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{SCRIPT_NAME}/{REQUEST_URI}" redirectType="SeeOther" />
</rule>
并在隧道尽头看到一些亮光. 更新3 非常感谢理查德的侦探!最新的答案让我也得到了调查,结果发现有一个与CookieApplyRedirectContext相关的few posts here on SO …所以现在这就是我所拥有的(这是我的具体情况),并且是我最初的追求: app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,LoginPath = new PathString("/Account/Login"),//This is why. If I could explicitly set this,then I (thought) I should
//be able to explicitly enforce https (too..as a setting)
//for the LoginPath...
CookieSecure = CookieSecureOption.Always,Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = .....,OnApplyRedirect = context =>
{
Uri absoluteUri;
if (Uri.TryCreate(context.RedirectUri,UriKind.Absolute,out absoluteUri))
{
var path = PathString.FromUriComponent(absoluteUri);
if (path == context.OwinContext.Request.PathBase + context.Options.LoginPath)
{
context.RedirectUri = context.RedirectUri.Replace("http:","https:");
}
}
context.Response.Redirect(context.RedirectUri);
}
}
});
解决方法出现此问题的原因是您的应用程序正在向绝对URL发出重定向.您可以在负载均衡器或应用程序本身中以两种方式之一解决此问题.负载均衡器 配置负载均衡器以重写从http到https的重定向响应.如果您使用的是ARR,则以下规则(取自here)应该有效: <rule name="forum-redirect" preCondition="IsRedirection" enabled="true">
<match serverVariable="RESPONSE_LOCATION" pattern="^http://[^/]+/(.*)" />
<conditions>
<add input="{ORIGINAL_HOST}" pattern=".+" />
</conditions>
<action type="Rewrite" value="http://{ORIGINAL_HOST}/{R:1}" />
</rule>
其他负载平衡器将需要类似的配置. 应用 我们可以使用相对URL替换OWIN在授权过程中重定向到的URL,这意味着协议将保留为以前使用的浏览器. 在Owin源代码中花了一些时间来寻找如何做到这一点,但是对应用程序启动的以下更改应该可以解决您的问题.首先,从启动配置中提取CookieAuthenticationProvider初始化. 更改: app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,Provider = new CookieAuthenticationProvider
{
// Move these options in the step below...
}
});
至: var cookieProvider = new CookieAuthenticationProvider
{
// ... Options from your existing application
};
// Modify redirect behaviour to convert login URL to relative
var applyRedirect = cookieProvider.OnApplyRedirect;
cookieProvider.OnApplyRedirect = context =>
{
if (context.RedirectUri.StartsWith("http://" + context.Request.Host))
{
context.RedirectUri = context.RedirectUri.Substring(
context.RedirectUri.IndexOf('/',"http://".Length));
}
applyRedirect(context);
};
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,Provider = cookieProvider
});
虽然我们无法轻易找到重定向规则的设置位置,但OWIN使用委托来执行实际的重定向.我在这里做的是存储该委托,修改它将要给出的URL,然后再次调用它. 使用此选项,确保您站点内的任何其他重定向和链接都是相对的. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc-3 – 如何使用MiniProfiler与单页Web应用程序/
- asp.net-mvc-3 – 错误:asp.net mvc3中当前上下文中不存在
- ASP.NET 2.0中的Gridview列宽
- asp.net-mvc – 域vs DTO vs ViewModel – 如何和何时使用它
- asp.net-mvc – 验证asp.net MVC中的只读输入不显眼的验证
- asp.net – 如何在MVC4的部分视图中添加脚本?
- asp.net-web-api – 如何在HttpReponseMessage上设置响应co
- WCF WebServiceHostFactory MaxReceivedMessageSize配置
- asp.net – ASP .NET核心Cookie身份验证到期时返回时从时间
- asp.net – 使用ItemType进行强类型转发器控制?
- asp.net-mvc – Sitecore在我的MVC解决方案中提供
- ASP.NET GridView SortedAscendingHeaderStyle不
- asp.net – 使用HtppWebRequest发布表单数据没有
- 学习Asp.Net WebForms或Asp.Net MVC
- asp.net – VS 2010失败调试:HttpException在Lo
- asp.net – 无法在动态创建的System.Web.UI.WebC
- asp.net – 从wsdl生成webservice
- asp.net – 我可以在Visual Studio 2008中使用MV
- asp.net – 工作线程和I/O线程有什么区别?
- asp.net – 如何设置sqldatasource参数的值?
