2015-04-24 18 views
1

我正在創建一個用於使用IdentityServer 3的測試應用程序,並且我正在使用AngularJs,Angular Ui路由器和oauth-ng庫來創建此應用程序。我的應用程序需要在客戶端使用OpenID連接授權碼流。所以我對oauth-ng庫做了很小的修改。我正在使用oauth-ng lib從IdentityServer3中檢索授權代碼,並且使用$ http.post()將該代碼發送到令牌端點並獲取access_token。

以下是我的代碼..

的JavaScript

.controller('LoginController', function ($scope, $http, $timeout, $location) { 

     var getParamsFromUrl = function(url) { 
      var splitted = url.split('?'); 
      splitted = splitted[1].split('&'); 
      var params = {}; 

      for (var i = 0; i < splitted.length; i++) { 
       var param = splitted[i].split('='); 
       var key = param[0]; 
       var value = param[1]; 
       params[key] = value 
      } 
      return params; 
     }; 

     var getToken = function (url, data) { 
      return $http.post(url, data); 
     }; 



     if($location.absUrl().split('?')[1]) { 
      $scope.params = getParamsFromUrl($location.absUrl()); 

      var tokenData = {}; 
      tokenData.grant_type = 'authorization_code'; 
      tokenData.code = $scope.params.code; 
      tokenData.redirect_uri = 'http://localhost:8000/login.html'; 

      var tokenEndpoint = 'https://localhost:44333/core/connect/token'; 

      getToken(tokenEndpoint, tokenData) 
       .then(function (data, status, headers, config) { 
        console.log(data); 
        console.log(status); 
        console.log(headers); 
        console.log(config); 
       }); 

     } 
    }); 

授權碼檢索成功,當我嘗試連接到令牌端點,它拋出下面的錯誤在瀏覽器上。

OPTIONS https://localhost:44333/core/connect/token b @ angular.min.js:87N @ angular.min.js:82 $ @ get.f angular.min.js:80(匿名函數)@ angular.min.js: 112 $ get.n. $ eval @ angular.min.js:126 $ get.n. $ digest @ angular.min.js:123 $ get.n. $ apply @ angular.min.js:126(anonymous (匿名函數)@ (angular function) angular.min.js:250n.Callbacks.j @ jquery.min.js:2n.Callbacks.k.fireWith @ jquery.min.js:2n.extend.ready @ jquery.min.js:2I @ jquery。 MI n.js:2

XMLHttpRequest無法加載https://localhost:44333/core/connect/token。 請求的 資源上沒有「Access-Control-Allow-Origin」標題。原因'http://localhost:8000'因此不允許 訪問。迴應有HTTP狀態代碼405.

然後,我試着用這行代碼配置應用程序配置$ httpProvider ..我的配置看起來像這樣。

.config(function ($locationProvider, $httpProvider) { 
     $locationProvider.html5Mode(true); 
     $httpProvider.defaults.withCredentials = true; // <= Added this 
    }); 

仍然得到同樣的問題。我如何從客戶端解決這個問題?我可以從客戶端解決這個問題嗎?

+0

99%是服務器端配置。我碰到過好幾次,始終是未配置的服務器,首先嚐試檢查服務器端。 –

+1

CORS是一個服務器端issue.try http://enable-cors.org/ – charlietfl

+0

好了,現在我認爲是100%@charlietfl –

回答

0

相當肯定的代碼授予流是服務器端應用程序。你應該只使用代碼授予,如果你可以保持客戶端的祕密「祕密」!

是他們不使用隱式流的原因嗎?這樣你就可以在重定向中獲得令牌而不是令牌。

在此頁面上解釋了流程。http://connect2id.com/learn/openid-connect

相關問題