我試用adal.js與從外部Web API站點(不同域)獲取數據的Angular SPA(單頁應用程序)網站。使用adal.js可以方便地對SPA進行身份驗證,但如果需要使用持證人令牌,則無法通過adal.js與API進行通信。除了無數的博客外,我還使用https://github.com/AzureAD/azure-activedirectory-library-for-js作爲模板。Adal.js沒有爲外部api端點資源獲取令牌
問題是,當我在啓動adal.js時設置端點時,adal.js似乎將所有輸出端點流量重定向到microsofts登錄服務。
觀察:
- Adal.js會話存儲包含兩個adal.access.token.key條目。一個用於SPA Azure AD應用程序的客戶端ID,另一個用於外部API。只有SPA令牌有一個值。
- 如果我不注入$ httpProvider到adal.js中,那麼調用將發送到外部API,並且返回401。
- 如果我手動將SPA令牌添加到http頭(授權:承載'令牌值'),我得到一個401作爲回報。
我的理論是adal.js無法檢索端點的令牌(可能是因爲我配置了一些錯誤的SPA)和停止交通的終點,因爲它是無法得到所需的令牌。 SPA令牌不能用於API,因爲它不包含所需的權限。 爲什麼adal.js沒有獲取端點的令牌,我該如何解決?
其他信息:
- 客戶端天青AD應用被配置爲使用委派針對API權限和oauth2AllowImplicitFlow =在應用清單真。
- API Azure AD應用程序配置爲模擬,oauth2AllowImplicitFlow = true(不要認爲這是必需的,但嘗試過)。這是多租戶。
- 該API配置爲允許所有CORS來源,並且在被其他Web應用程序使用模擬(混合MVC(Adal.net)+ Angular)使用時,它可以正常工作。
會話存儲:
key (for the SPA application): adal.access.token.keyxxxxx-b7ab-4d1c-8cc8-xxx value: eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ik1u...
key (for API application): adal.access.token.keyxxxxx-bae6-4760-b434-xxx
value:
app.js(角和阿達爾配置文件)
(function() {
'use strict';
var app = angular.module('app', [
// Angular modules
'ngRoute',
// Custom modules
// 3rd Party Modules
'AdalAngular'
]);
app.config(['$routeProvider', '$locationProvider',
function ($routeProvider, $locationProvider) {
$routeProvider
// route for the home page
.when('/home', {
templateUrl: 'App/Features/Test1/home.html',
controller: 'home'
})
// route for the about page
.when('/about', {
templateUrl: 'App/Features/Test2/about.html',
controller: 'about',
requireADLogin: true
})
.otherwise({
redirectTo: '/home'
})
//$locationProvider.html5Mode(true).hashPrefix('!');
}]);
app.config(['$httpProvider', 'adalAuthenticationServiceProvider',
function ($httpProvider, adalAuthenticationServiceProvider) {
// endpoint to resource mapping(optional)
var endpoints = {
"https://localhost/Api/": "xxx-bae6-4760-b434-xxx",
};
adalAuthenticationServiceProvider.init(
{
// Config to specify endpoints and similar for your app
clientId: "xxx-b7ab-4d1c-8cc8-xxx", // Required
//localLoginUrl: "/login", // optional
//redirectUri : "your site", optional
extraQueryParameter: 'domain_hint=mydomain.com',
endpoints: endpoints // If you need to send CORS api requests.
},
$httpProvider // pass http provider to inject request interceptor to attach tokens
);
}]);
})();
調用端點角碼:
$scope.getItems = function() {
$http.get("https://localhost/Api/Items")
.then(function (response) {
$scope.items = response.Items;
});
我想我可能有同樣的問題。你有沒有想到這一個? – Juha
沒有。我會讓你知道如果我知道它 –
開始在這一點上的賞金。我與你的情況完全相同,並嘗試過所有我能想到的事情,閱讀ADAL快速入門Gtithub網站上的所有信息,甚至購買了Vittorio的血腥書籍! 我只是不能相信,這樣一個簡單的情況根本不起作用! –