如何通过AngularJS $ http从ASP.Net Web API 2获取Access Token? [英] How to get Access Token from ASP.Net Web API 2 via AngularJS $http?

查看:110
本文介绍了如何通过AngularJS $ http从ASP.Net Web API 2获取Access Token?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试这样:

$http({ method: 'POST', url: '/token', data: { username: $scope.username, password: $scope.password, grant_type: 'password' } }).success(function (data, status, headers, config) {
    $scope.output = data;
}).error(function (data, status, headers, config) {
    $scope.output = data;
});

然后尝试将grant_type更改为参数:

then tried changing the grant_type to a param:

$http({ method: 'POST', url: '/token', data: { username: $scope.username, password: $scope.password }, params: { grant_type: 'password' } }).success(function (data, status, headers, config) {
    $scope.output = data;
}).error(function (data, status, headers, config) {
    $scope.output = data;
});

仍然可怕: {error:unsupported_grant_type}

所以我做了AngularJS开发人员应该做的事情,使用jQuery:

So I do what no AngularJS developer should ever do, resorted to jQuery:

var data = $('#regForm').serialize() + "&grant_type=password";
$.post('/token', data).always(showResponse);

function showResponse(object) {
    $scope.output = JSON.stringify(object, null, 4);
    $scope.$apply();
};

哪个像冠军一样...所以我的问题是:我们如何复制jQuery $。post()使用AngularJS $ http()调用上面所以我们可以从OWIN中间件中获取访问令牌/ ASP.Net Web API 2中的令牌端点?

Which works like a champ... so my question is: how do we replicate the jQuery $.post() call above using AngularJS $http() so we can grab an access token from the OWIN middleware based /token endpoint in ASP.Net Web API 2?

推荐答案

执行此操作:

$http({
        url: '/token',
        method: 'POST',
        data: "userName=" + $scope.username + "&password=" + $scope.password + 
              "&grant_type=password"
})

这篇关于如何通过AngularJS $ http从ASP.Net Web API 2获取Access Token?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆