无法在标头AngularJs中发送用户名和密码

时间:2016-10-03 09:22:39

标签: angularjs http

我正在尝试使用以下代码

在http标头中发送用户名和密码
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' : 'p@ssw@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});
    }
});

但它似乎对我不起作用。我只是想尝试实现基本身份验证。这是我在.net中使用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);
        }

如果我要进行身份验证,那么数据在标头中不可用,因此会收到无效的用户名或密码错误。

1 个答案:

答案 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' : 'p@ssw@rd'
 }
};
$http(req).then(function(response){
    $scope.branchs = response.data;
}, function(data, status, headers, config){
    console.log('Api call failed', status);
});