首页 > 解决方案 > 使用邮递员时,身份验证标头在 Angular 1.1.x 到 1.5.x 版本中不起作用

问题描述

我的 Web API 2.-0 代码是 -

OnValidateClientAuthentication = async context =>
{

.......string clientId;

........string clientSecret;

........if (context.TryGetBasicCredentials(out clientId, out clientSecret))

........{

..................if (clientId == "client1" && clientSecret == "secretKey1")

................{

....................context.Validated();

................}

...........}

}

我在这里使用承载身份验证。现在我从角度调用这个-

尝试了两种方法-

方法1。

在模块配置中 -

app.config(['$httpProvider', function ($httpProvider) 
{

$httpProvider.defaults.headers.common['Authorization'] = 'Basic ' + window.btoa("Client1:secretKey1");

}

方法2。

app.service('memberService', function ($http)

{

var headers = 
{ 

'Access-Control-Allow-Origin': true, 

'X-Requested-With': 'XMLHttpRequest', 

'Content-Type': 'application/x-www-form-urlencoded', 

'Authorization':'Basic ' + window.btoa("Client1:secretKey1") 


}

this.GetTocken = function (data) 

{


var response = $http({

    method: "Post",

    url: window.g_baseUrl + "accesstoken",

    headers: headers

});

return response;

};

}

我尝试使用 Angular 1.1.x 到 1.5.x,但没有成功。谷歌告诉 HttpClient 类是完全的。设置授权标头后愚蠢,两个键:访问控制请求方法和访问控制请求标头正在增加,但没有名称为“授权”的键。从邮递员发送请求时,在请求标头中添加授权密钥。

标签: angularjsoauthauthorizationasp.net-web-api2access-token

解决方案


推荐阅读