2016-10-03 38 views
0

我試圖使用HTTP頭中發送用戶名和密碼,下面的代碼不能在頭AngularJs

var app = angular.module("BuildApp",[]); 

app.controller("BranchController", function ($scope, $http, $rootScope) { 
    var config = { 
     headers : { 
      'contentType': "application/json; charset=utf-8",//required 
      'Username' : 'myUser', 
      'Password' : '[email protected]@rd' 
     } 
    } 
    $http.get('http://192.168.3.96:8082/build-api/v1.1/builds/getbranch', config). 
    success(function (data, status, headers, config) { 
     $scope.branchs = data; 
    }). 
    error(function (data, status, headers, config) { 
     console.log('Api call failed', status) 
    }); 
    $scope.flag = false; 
    $scope.selectedBranch = 'Nothing Selected'; 
    $scope.dropboxitemselectedbranch = function (item) { 
     $scope.selectedBranch = item; 
     $rootScope.branch = item; 
     $rootScope.$emit('changeName',{selectedBranch:$scope.selectedBranch}); 
    } 
}); 

發送用戶名和密碼,但它似乎並不爲我工作。我只是試圖實現基本認證。這裏是owin

public override async Task Invoke(IOwinContext context) 
     { 
      int vNum = 0, rNum = 0; 

      IOwinResponse response = context.Response; 

      try 
      {   
       bool gotAuthenicated = await Task.Run(() => 
       { 
        try 
        { 
         if (context.Request.Headers != null) 
         { 
          if (context.Request.Headers.Keys.Contains("username") && 
           context.Request.Headers.Keys.Contains("password")) 
          { 
           string username = context.Request.Headers.Get("username"); 
           string password = context.Request.Headers.Get("password"); 
           if (username == _authenDictionary["Username"] && 
            password == _authenDictionary["Password"]) 
            return true; 
          } 

          return false; 
         } 
         return false; 
        } 
        catch 
        { 
         // ignored 
        } 
        return false; 
       }); 

       if (!gotAuthenicated) 
       { 
        context.Response.StatusCode = (int)HttpStatusCode.NotFound; 
        await context.Response.WriteAsync("UserName or Password is Incorrect"); 
        return; 
       } 



      } 
      catch 
      { 
       context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; 

       return; 
      } 

      context.Set(BuildConstants.ENGINE_CONFIG_KEY, _config); 
      await Next.Invoke(context); 
     } 

的數據是不是在頭可如果我要因此驗證了無效的用戶名或密碼錯誤在.NET認證我的後端代碼。

+0

它應該是Content-Type的 –

+0

@Ravi謝謝,但仍面臨着同樣的問題 –

+0

你得到什麼迴應回來?你能檢查瀏覽器網絡標籤,看看發生了什麼.. –

回答

0

嘗試打破請求:

var req = { 
method: 'GET', 
url: 'http://192.168.3.96:8082/build-api/v1.1/builds/getbranch', 
headers: { 
    'Content-Type': 'application/json; charset=utf-8', 
    'Username' : 'myUser', 
    'Password' : '[email protected]@rd' 
} 
}; 
$http(req).then(function(response){ 
    $scope.branchs = response.data; 
}, function(data, status, headers, config){ 
    console.log('Api call failed', status); 
});