2016-02-25 192 views
2

我正在構建使用Ionic框架的iOS移動應用程序。該應用程序將訪問由使用集成Windows身份驗證在IIS上託管的ASP.NET 5(MVC 6)應用程序提供的API。服務器已經有一個使用AngularJS客戶端的Web界面。我一直試圖從Ionic/Angularjs控制器中獲得$ http調用服務器,並且沒有通過IIS集成Windows身份驗證(我嘗試在設備/模擬器以及離子服務器上運行)運行。我總是得到一個401未經授權的錯誤。我嘗試過設置withCredentials爲true,並在請求中輸入用戶名/密碼,但沒有運氣。當我嘗試從iPhone(非Windows環境)的Safari瀏覽器訪問API URL時,我確實得到了瀏覽器身份驗證彈出窗口,該窗口在輸入我的Intranet窗口用戶名密碼時成功登錄。Ionic NTLM身份驗證 - IIS

我最初有一些CORS問題,我通過在服務器端添加CORS服務並允許所有來源進行排序。我還有代理設置,以避免在使用離子服務進行測試時發生CORS問題。有沒有人做過這樣的事情?這是我的控制器代碼:

angular.module('starter.controllers', []) 

.controller('AppCtrl', function($scope, $ionicModal, $http) { 
$http.defaults.useXDomain = true; 
    $http.defaults.withCredentials = true; 
    // Form data for the login modal 
    $scope.loginData = {}; 
    // Create the login modal that we will use later 
    $ionicModal.fromTemplateUrl('templates/login.html', { 
    scope: $scope 
    }).then(function(modal) { 
    $scope.modal = modal; 
    }); 
    // Triggered in the login modal to close it 
    $scope.closeLogin = function() { 
    $scope.modal.hide(); 
    }; 
    // Open the login modal 
    $scope.login = function() { 
    $scope.modal.show(); 
    }; 

    // Perform the login action when the user submits the login form 
    $scope.doLogin = function() { 
    console.log('Doing login', $scope.loginData); 
    $http.post('http://localhost:8100/api/APIAccount/Login',{withCredentials:true}) 
    .then(function(response) 
{ 
    console.log('success'); 
}, function(error) { 
    console.log('error'); 
}); 

    }; 
}); 

回答

2

幾個小時後故障排除,這是因爲建立ASP.NET 5 CORS服務,讓憑證一樣簡單。在ConfigureServices函數的Startup.cs文件中,我必須輸入以下內容。希望這有助於未來的其他人。

services.AddCors(options => 
       { 
       options.AddPolicy("AllowAllOrigins", 
       builder => builder.WithOrigins("http://<domainname>") 
       .AllowCredentials()); 
       });