我尝试使用Jquery Ajax进行基本身份验证。我向这样的ajax请求添加授权标头。
$.ajax({
headers: {
"authorization": "basic 123456",
},
crossDomain: true,
type: "POST",
contentType: "application/json; charset=utf-8",
url: 'http://example.com/Auth.asmx/Authorization',
data: null,
dataType: "jsonp",
success: function (data, status) {
console.log("Success");
console.log(data);
},
error: function (xmlRequest) {
console.log("Error");
console.log(xmlRequest);
}
});
然后在ASP.Net Web服务中,我尝试获取此标头。
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public void Authorization(string callback)
{
string headers = GetRequestHeaders();
}
public string GetRequestHeaders()
{
HttpContext ctx = HttpContext.Current;
if (ctx == null || ctx.Request == null || ctx.Request.Headers == null)
{
return string.Empty;
}
string headers = string.Empty;
foreach (string header in ctx.Request.Headers.AllKeys)
{
string[] values = ctx.Request.Headers.GetValues(header);
headers += string.Format("{0}: {1}", header, string.Join(",", values));
}
return headers;
}
打印到标题时,看不到授权标题
缓存控制:无缓存
连接:保持活动状态
语法:无缓存
接受: /
接受编码:gzip,deflate
接受语言:tr-TR,tr; q = 0.9,en-US; q = 0.8,en; q = 0.7
主持人:example.com
推荐人:http://localhost:9200/test.html
用户代理:Mozilla / 5.0 (Windows NT 10.0; Win64; x64)AppleWebKit / 537.36(KHTML,如Gecko) Chrome / 78.0.3904.108 Safari / 537.36
为什么我看不到授权标头,如何获得此值?
P.S。我在客户端使用Jquery 2.2.3
我尝试了一些不同的尝试。我在本地创建了Node JS服务器,而不是向它发送相同的请求。授权标头仍然无法显示。
我使用了应用程序“邮递员”来创建并发送请求。我添加了授权标头,然后发送到我的服务器(ASP.Net和Node JS)。 我在此应用程序的请求中看到了授权标头。
对于ASP.Net,输出为
缓存控制:无缓存
连接:保持活动状态
接受: /
接受编码:gzip,放气
授权:基本12345
主机:192.168.1.100
用户代理:PostmanRuntime / 7.17.1
邮递员令牌:5911bb3a-ea8a-4f81-8579-7e6aed3ced61
然后,我从Postman应用程序获取Jquery AJAX输出以创建此请求。
var settings = {
"async": true,
"crossDomain": true,
"url": "http://192.168.1.107:1800",
"method": "GET",
"headers": {
"Authorization": "Basic 12345",
"User-Agent": "PostmanRuntime/7.17.1",
"Accept": "*/*",
"Cache-Control": "no-cache",
"Postman-Token": "5911bb3a-ea8a-4f81-8579-7e6aed3ced61,3455808c-4a4f-4108-b819-f061b5a8e37e",
"Host": "192.168.1.100",
"Accept-Encoding": "gzip, deflate",
"Connection": "keep-alive",
"cache-control": "no-cache"
}
}
$.ajax(settings).done(function (response) {
console.log(response);
});
我这样更改了Ajax设置。
$.ajax({
"async": true,
"crossDomain": true,
"url": "http://192.168.1.107:1800",
"method": "GET",
"headers": {
"Authorization": "Basic 12345",
"User-Agent": "PostmanRuntime/7.17.1",
"Accept": "*/*",
"Cache-Control": "no-cache",
"Postman-Token": "5911bb3a-ea8a-4f81-8579-7e6aed3ced61,3455808c-4a4f-4108-b819-f061b5a8e37e",
"Host": "192.168.1.100",
"Accept-Encoding": "gzip, deflate",
"Connection": "keep-alive",
"cache-control": "no-cache"
},
success: function (data, status) {
console.log("Success");
console.log(data);
callback("Authorization OK");
},
error: function (xmlRequest) {
console.log("Error");
console.log(xmlRequest);
callback("Authorization Error");
}
});
当我向服务器发送相同的请求时,我再也看不到授权标头。
我的请求的标题
“主机”:“ 192.168.1.107:1800”,
“连接”:“保持活动”,
“ pragma”:“ no-cache”,“ cache-control”:“ no-cache”,
“访问控制请求方法”:“获取”,
“来源”:“ http://localhost:9200”,
“用户代理”:“ Mozilla / 5.0(Windows NT 10.0; Win64; x64)AppleWebKit / 537.36(KHTML,例如Gecko)Chrome / 78.0.3904.108 Safari / 537.36”,
“访问控制请求标头”:“授权,缓存控制,邮递员令牌”,
“接受”:“ / ”,
“引荐来源”:“ http://localhost:9200/main.html”,
“ accept-encoding”:“ gzip,deflate”,
“ accept-language”:“ tr-TR,tr; q = 0.9,en-US; q = 0.8,en; q = 0.7”
答案 0 :(得分:0)
试试这个兄弟:
JS代码(AJAX)
$.ajax({
crossDomain: true,
type: "POST",
beforeSend: function (xhr) {
xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
xhr.setRequestHeader('Authorization', 'Hello 123456');
},
contentType: "application/json; charset=utf-8",
url: 'http://example.com/Auth.asmx/Authorization',
data: "{'callback':'test'}",
dataType: "jsonp",
success: function (data, status) {
console.log("Success");
console.log(data);
},
error: function (xmlRequest) {
console.log("Error");
console.log(xmlRequest);
}
});
,您可以使用以下命令读取值:
C#代码
string auth = HttpContext.Current.Request.Headers["Authorization"];
注意::如果这样做不起作用,请尝试在您的IIS版本中配置跨源请求,您可以根据需要从Web.config
或IIS管理面板中进行配置版本:https://enable-cors.org/server_iis7.html