【发布时间】:2015-11-03 23:30:40
【问题描述】:
我正在尝试将 IdentityServer 与 Angularjs 应用程序一起使用。 Angularjs 前端旨在在未经身份验证的用户尝试导航到应用程序的根 url 时调用授权端点。 app.config 有一个 resolve 属性,它会生成一个 routeChangeError。此事件触发重定向到 OAuth2 授权端点(在我的例子中是 IdentityServer,在 Asp.Net MVC 上运行)。
baseballApp.config(['$routeProvider', '$locationProvider', '$httpProvider', function($routeProvider, $locationProvider, $httpProvider) {
$routeProvider.when('/players', {
resolve: {
auth: function ($q, $rootScope) {
var deferred = $q.defer();
if ($rootScope.token) {
deferred.resolve();
} else {
deferred.reject();
}
return deferred.promise;
}
},
templateUrl: '/FrontEnd/Templates/PlayerList.html',
controller: 'PlayerListController'
});
$routeProvider.otherwise({ redirectTo: '/players' });
var spinnerFunction = function (data, headersGetter) {
if (data) {
$('#spinner').show();
return data;
}
};
$httpProvider.defaults.transformRequest.push(spinnerFunction);
$httpProvider.interceptors.push('authInterceptor');}]).run(['$rootScope', '$location', 'authenticationService', function ($rootScope, $location, authenticationService) {
//authenticationService.getBearerToken().then(function(data) {
// $rootScope.token = data;
//});
debugger;
var generateRandomString = function() {
return (Math.random().toString(16) + "000000000").substr(2, 8);
}
var nonce = generateRandomString();
var state = generateRandomString();
$rootScope.$on('$routeChangeError', function () {
window.location = 'https://localhost:44301/identity/connect/authorize?client_id=baseballStats&redirect_uri=https%3a%2f%2flocalhost%3a44300%2f&response_type=id_token+token&scope=openid+profile+roles+baseballStatsApi&nonce=' + nonce + '&state=' + state;
});}]);
如您所见,如果我没有令牌,我会重定向到授权端点。但是,我不清楚在授权端点上登录后如何检索令牌。
我已经指定了一个返回 url,在登录后它会重定向到应用程序,但是我将如何设置我的角度代码来检索应该在授权端点上生成并发送回应用程序的令牌?
我见过很多人使用不记名令牌身份验证进行 Angular 的示例,但我还没有看到任何人将 OAuth2 服务器的授权端点与 Angular 一起使用。在 MVC 中,有许多内置的回调来检索令牌。我正在寻找一些关于如何以角度实现这一点的想法。
你能告诉我我做错了什么吗?
编辑
我还尝试在重定向 uri 中传递哈希导航标记,但从 IdentityServer 获得“错误未知客户端”。请看下面的代码
$rootScope.$on('$routeChangeError', function () {
var returnUrl = encodeURIComponent('https://localhost:44300/#/players');
window.location = 'https://localhost:44301/identity/connect/authorize?client_id=baseballStats&redirect_uri=' + returnUrl + '&response_type=id_token+token&scope=openid+profile+roles+baseballStatsApi&nonce=' + nonce + '&state=' + state;
});
有人知道为什么吗?
【问题讨论】:
标签: javascript asp.net-mvc angularjs oauth-2.0 owin