2016-09-19 84 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 }