2016-09-19 80 views
1

当我将此令牌发送到WebApi中的控制器时,我得到401(未授权)信息为什么?我把这个令牌形式登录并且它是正确的。表单身份验证 - 令牌发送到控制器

$scope.expiresCookie = $cookies.get('expires'); 
$scope.issuedCookie = $cookies.get('issued'); 
$scope.access_tokenCookie = $cookies.get('access_token'); 
$scope.expires_inCookie = $cookies.get('expires_in'); 
$scope.token_typeCookie = $cookies.get('token_type'); 
$scope.user_nameCookie = $cookies.get('user_name'); 


$scope.addLink = "http://localhost:56305/api/ProductInsuranceCar"; 


$http({ 
    method: "GET", 
    url: $scope.addLink, 
    headers: { 'Authorization': '' + $scope.token_typeCookie + ' ' + $scope.access_tokenCookie + '' }, 
    headers: { 'Content-Type': 'application/json; charset=utf-8' } 
}).then(function mySucces(response) { 
    $scope.test = response; 
}, function myError(response) { 
}); 

图片1

enter image description here

图片2

enter image description here

实施例正确的请求

enter image description here

当我使用高级REST客户端,我得到的信息,但在我的方法:/不工作...

enter image description here

回答

0

问题可能出在从不同的UI域调用Web API时。

如果域不同,则不会发送Cookie。因此,如果您拨打电话localhost:64757localhost:56305不发送Cookie,那就是您收到Unathorized 401错误的原因。

1

正确答案。标题的位置很重要:/

headers: { 'Content-Type': 'application/json; charset=utf-8' }, 
headers: { 'Authorization': $scope.Authorization2 }