且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

AD FS 2.0身份验证和AJAX

更新时间:2022-12-11 17:17:28

您有两种选择。 更多信息这里。

You have two options. More info here.

首先是共享入境申请(一个基于该HTML)和你的API解决方案之间的会话cookie。您配置这两个应用程序使用相同的WIF的cookie。这只有当这两个应用程序在相同的根域。 见上面的帖子或本计算器问题

The first is to share a session cookie between an entry application (one that is HTML based) and your API solutions. You configure both applications to use the same WIF cookie. This only works if both applications are on the same root domain. See the above post or this *** question.

另一种选择是禁用passiveRedirect对AJAX请求(如由Gutek显示在上面的回答)。这将返回401,你可以处理在Javascript中一个HTTP状态code。 当你发现了401,你加载一个虚拟页面(或认证对话框,它可能会增加一倍作为一个登录对话框,如果证书需要再次给出),在一个iFrame。当iFrame的完成你然后再次尝试呼叫。这一次的会话cookie将在电话会议上present,它应该会成功。

The other option is to disable the passiveRedirect for AJAX requests (as shown in above answer by Gutek). This will return a http status code of 401 which you can handle in Javascript. When you detect the 401, you load a dummy page (or a "Authenticating" dialog which could double as a login dialog if credentials need to be given again) in an iFrame. When the iFrame has completed you then attempt the call again. This time the session cookie will be present on the call and it should succeed.

    //Requires Jquery 1.9+


var webAPIHtmlPage = "http://webapi.somedomain/preauth.html"

function authenticate() {


  return $.Deferred(function (d) {


    //Potentially could make this into a little popup layer 
    //that shows we are authenticating, and allows for re-authentication if needed
    var iFrame = $("<iframe></iframe>");
    iFrame.hide();
    iFrame.appendTo("body");
    iFrame.attr('src', webAPIHtmlPage);


    iFrame.load(function () {

      iFrame.remove();
      d.resolve();
    });

  });

};



function makeCall() {

    return $.getJSON(uri)
                .then(function(data) {

                    return $.Deferred(function(d) { d.resolve(data); });

                    },
                   function(error) {


                       if (error.status == 401) {
                           //Authenticating, 
                           //TODO:should add a check to prevnet infinite loop
                           return authenticate().then(function() {
                               //Making the call again
                               return makeCall();

                           });
                       } else {
                           return $.Deferred(function(d) {
                               d.reject(error);
                           });
                       }

                   });


}