在我的MVC Web应用程序中使用WsFederation Authentication
时遇到登录循环问题。我使用visual studio来创建Web应用程序的脚手架,并在WsFederation
中设置Startup.cs
。这会产生以下代码块:
public class Startup
{
private static string realm = ConfigurationManager.AppSettings["ida:Wtrealm"];
private static string adfsMetadata = ConfigurationManager.AppSettings["ida:ADFSMetadata"];
public void Configuration(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseWsFederationAuthentication(new WsFederationAuthenticationOptions
{
Wtrealm = realm,
MetadataAddress = adfsMetadata
});
}
}
Web应用程序托管在Azure中,ADFS位于内部。
在某些客户端上,当进行登录尝试时,登录页面进入循环,请求新的令牌在ADFS服务器上导致以下异常:
异常详情: Microsoft.IdentityServer.Web.InvalidRequestException:MSIS7042:相同的客户端浏览器会话在最后“7”秒内发出了“6”个请求。有关详细信息,请与您的管理员联系。
我已经阅读了很多关于StackOverflow的文章,并查看了编写IdentityServer的人提供的各种示例,我尝试了各种配置选项,但我无法将问题隔离到特定区域。
从我读到的内容来看,OWIN中间件丢失了对象的上下文,因此令牌“丢失”。
我试图实现其他在StackOverflow上提供的sample code但是,我似乎找不到解决方案来解决我的问题,或者可能没有正确实现代码。
任何想法?
答案 0 :(得分:5)
问题的原因是请求和响应URL不一样。即当用户输入网站URL并且没有使用HTTPS作为前缀时,将发生重定向循环。
原因是隐藏的,因为如果未经过身份验证或授权,用户会立即重定向到ADFS。
我所要做的就是确保所有用户请求都被重定向回HTTPS网址,并且删除了HTTP绑定。(其中任何一种工作都可以正常工作)
这是我用来确保所有请求都重定向到https的代码。
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to https">
<match url="(.*)"/>
<conditions>
<add input="{HTTPS}" pattern="Off"/>
<add input="{REQUEST_METHOD}" pattern="^get$|^head$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}"/>
</rule>
</rules>
</rewrite>
</system.webServer>
我希望这篇文章有用。