2016-02-25 220 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()); 
       });