简体   繁体   English

在受WIF(PassiveAuthentication)保护的MVC应用程序中,使用ajax请求加载部分视图

[英]Loading a partial view with an ajax request in an MVC application secured with WIF (PassiveAuthentication)

I am developing an MVC4 application secured with Windows Identity Foundation. 我正在开发受Windows Identity Foundation保护的MVC4应用程序。

I am also using jQuery ( $.ajax() ) to refresh some partial views at regular intervals. 我还使用jQuery($ .ajax())定期刷新一些局部视图。

This application is used for monitoring some systems and no navigation occurs apart from the partial view refreshes. 此应用程序用于监视某些系统,除了部分视图刷新之外,没有导航发生。

After 20 minutes the WIF security cookie expires and my application tries to request another token from the PassiveSTS page. 20分钟后,WIF安全cookie到期,我的应用程序尝试从PassiveSTS页面请求另一个令牌。 As expected, the PassiveSTS performs authentication and redirects to the calling URL. 如预期的那样,PassiveSTS执行身份验证并重定向到调用URL。

The problem is that the ajax() request used to load the partial view expects an HTTP 200 code and receives HTTP 302 instead. 问题在于,用于加载部分视图的ajax()请求期望使用HTTP 200代码并接收HTTP 302。

It seems to me that Passive authentication is not designed to work properly with ajax calls to partial views. 在我看来,被动身份验证并未设计为与对部分视图的Ajax调用一起正常工作。

What is wrong with this approach? 这种方法有什么问题? Is an active authentication more appropriate? 主动身份验证是否更合适?

Ajax calls should issue 401 if the caller is not authorized. 如果未授权调用方,则Ajax调用应发出401。 The problem is that WIF assumes the request is a browser and does the redirect. 问题是WIF假定该请求是浏览器并进行重定向。 I built a helper to suppress these redirects for Ajax calls in Thinktecture IdentityModel. 我构建了一个助手来抑制Thinktecture IdentityModel中针对Ajax调用的这些重定向。 You can either use the library from NuGet or just grab the relevant code and add it to your project. 您可以使用NuGet中的库,也可以只获取相关代码并将其添加到您的项目中。

http://brockallen.com/2013/02/19/suppress-login-redirects-for-api-clients-in-wif-with-thinktecture-identitymodel/ http://brockallen.com/2013/02/19/suppress-login-redirects-for-api-clients-in-wif-with-thinktecture-identitymodel/

https://github.com/thinktecture/Thinktecture.IdentityModel.45/ https://github.com/thinktecture/Thinktecture.IdentityModel.45/

Of course your client side JS would then have to do something when it gets the 401, but presumably you'd inform the use that you need to log back in. 当然,当您的客户端JS获得401时,它就必须做一些事情,但是想必您会告知使用者您需要重新登录。

Another thing might be to use sliding sessions, yet another feature I added to IdentityModel: 另一件事可能是使用滑动会话,而我添加到IdentityModel的另一个功能是:

http://brockallen.com/2013/02/17/sliding-sessions-in-wif-with-the-session-authentication-module-sam-and-thinktecture-identitymodel/ http://brockallen.com/2013/02/17/sliding-sessions-in-wif-with-the-session-authentication-module-sam-and-thinktecture-identitymodel/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM