[英]How to handle log out and redirect
使用.NET Framework 4.5创建了将使用AngularJS的SPA应用程序。 我按照指令实现了System.IdentityModel,以指向第三方身份验证。
Web.config编辑:
<configSections>
<section name="system.identityModel" type="System.IdentityModel.Configuration.SystemIdentityModelSection, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
<section name="system.identityModel.services" type="System.IdentityModel.Services.Configuration.SystemIdentityModelServicesSection, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
</configSections>
<location path="FederationMetadata">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
...
<system.web>
<authorization>
<deny users="?" />
</authorization>
<authentication mode="None" />
<compilation debug="true" targetFramework="4.5" />
<httpRuntime requestValidationType="ourcustomvalidator, ourcustomnamespace" />
<pages controlRenderingCompatibilityVersion="4.0" validateRequest="false" />
</system.web>
....
<modules runAllManagedModulesForAllRequests="true">
<remove name="FormsAuthentication" />
<add name="WSFederationAuthenticationModule" type="System.IdentityModel.Services.WSFederationAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" preCondition="managedHandler" />
<add name="SessionAuthenticationModule" type="System.IdentityModel.Services.SessionAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" preCondition="managedHandler" />
</modules>
...
因此,在首次启动单页应用程序时,该站点将重定向到授权站点。 从那里您登录,授权,然后将您重定向回该应用程序。
现在,我有一个WebAPI Restful部分,它是解决方案的一部分,还可以记录客户端错误以及处理应用程序中的注销。 这导致我遇到了一些我没有抓住的问题:
1)如果我对WebApi进行了/ api / logoff调用,则可以调用FederatedAuthentication.SessionAuthenticationModule.SignOut(); 而且我在幕后退出。 使用角度,我应该如何重定向用户? 我发出此命令后注意到,如果我按F5键,则刷新了我的站点,但我会自动重新登录。我希望将其返回到初始页面加载时的登录屏幕。
2)如果我对WebApi进行了/ api / custom调用,但是我在后台退出了,该如何捕获并重定向用户? 现在,我收到以下错误消息:
XMLHttpRequest cannot load https://mycustomloginurl.com/?wa=wsignin1.0&wtrealm=http%3a%2f%2flocalhost%3a561…%3dpassive%26ru%3d%252fwebapi%252fims%252ftesting&wct=2014-02-21T21%3a47%3a09Z.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:56181' is therefore not allowed access.
抱歉,这很令人困惑,我想把所有这些都缠住。
感谢更多研究,这篇文章: .Net MVC WS联合站点中的防止XmlHttpRequest重定向响应帮助我找到了正确的答案。
基本上,我遵循相同的代码,但添加了以下内容:
resp.Clear(); // cleared the response
var fa = FederatedAuthentication.WSFederationAuthenticationModule;
var signInRequestMessage = new SignInRequestMessage(new Uri(fa.Issuer), fa.Realm);
var signInURI = signInRequestMessage.WriteQueryString();
resp.Write(signInURI);
因此,我清除了响应,并使响应的主体包含用于身份验证的登录URL。 在角度上,我有一个HTTP拦截器,该拦截器检查状态码401,然后使用上述数据重定向到登录名。
app.config([
'$httpProvider', function($httpProvider) {
var interceptor = ['$rootScope', '$q','$window', function (scope, $q, $window) {
function success(response) {
return response;
}
function error(response) {
var status = response.status;
if (status == 401) {
$window.location.href = response.data.replace(/"/g, "");
}
// otherwise
return $q.reject(response);
}
return function (promise) {
return promise.then(success, error);
}
}];
$httpProvider.responseInterceptors.push(interceptor);
}
]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.