【发布时间】:2016-06-08 23:00:32
【问题描述】:
我正在使用 Ionic 框架构建一个 iOS 移动应用程序。该应用程序将访问 API,这些 API 将由使用集成 Windows 身份验证的 IIS 上托管的 ASP.NET 5 (MVC 6) 应用程序提供服务。服务器已经有一个使用 AngularJS 客户端的 Web 界面。我一直在尝试从 Ionic/Angularjs 控制器中获取对服务器的 $http 调用,并且没有通过 IIS 集成 Windows 身份验证(我尝试在设备/模拟器以及 ionic serve 上运行)。我总是收到 401 Unauthorized 错误。我尝试将 withCredentials 设置为 true 并在请求中传入用户名/密码,但没有成功。当我尝试从 iPhone(非 Windows 环境)上的 safari 访问 API URL 时,我确实得到了浏览器身份验证弹出窗口,它在输入我的 Intranet windows 用户名密码时成功登录。
我最初遇到了一些 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');
});
};
});
【问题讨论】:
标签: ios asp.net angularjs iis ionic-framework