Try*_*tch 19 javascript ajax jquery ntlm web-services
我正在开发一个与之通信的HTML5移动应用程序WebServices
.WebServices使用NTLM身份验证协议.我很难通过JavaScript处理握手.NTLM将401 unauthorized
响应发送到我的POST,我没有找到任何回复的方法.
JavaScript可以进行NTLM身份验证吗?我应该使用例如基本身份验证来构建代理Web服务吗?
我的jQuery调用就像......
$.ajax({
type: "POST",
url: URL,
contentType: "text/xml",
dataType: "xml",
data: soapRequest,
username: 'username',
password: 'password',
xhrFields: {
withCredentials: true
},
success: processSuccess,
error: processError
});
Run Code Online (Sandbox Code Playgroud)
您无需响应NTLM(集成Windows身份验证)挑战,如果配置正确,浏览器应为您完成。许多其他并发症也是可能的。
第1步-浏览器
检查浏览器是否可以使用NTLM Web应用程序或直接点击您正在直接开发的软件来访问和发送您的凭据。
第2步-JavaScript withCredentials属性
当我未能将“ withCredentials”属性设置为“ true”时,收到的401未经授权错误和所描述的症状完全相同。我对jQuery不熟悉,但是请确保您成功设置该属性。
这个例子对我有用:
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "https://localhost:44377/SomeService", true);
xhttp.withCredentials = true;
xhttp.send();
xhttp.onreadystatechange = function(){
if (xhttp.readyState === XMLHttpRequest.DONE) {
if (xhttp.status === 200)
doSomething(xhttp.responseText);
else
console.log('There was a problem with the request.');
}
};
Run Code Online (Sandbox Code Playgroud)
第3步-服务器端启用CORS(可选)
我怀疑人们最终想到这个问题的主要原因是他们正在工作站上开发一个组件,而在其他地方托管另一个组件。这会导致跨域资源共享(CORS)问题。有两种解决方案:
简而言之,要使用凭证启用CORS ,您必须:
这是我的global.asax文件中的工作.NET代码示例。我认为很容易看到正在发生的事情,并在需要时将其翻译为其他语言。
void Application_BeginRequest(object sender, EventArgs e)
{
if (Request.HttpMethod == "OPTIONS")
{
Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
Response.AddHeader("Access-Control-Max-Age", "1728000");
Response.End();
}
else
{
Response.AddHeader("Access-Control-Allow-Credentials", "true");
if (Request.Headers["Origin"] != null)
Response.AddHeader("Access-Control-Allow-Origin" , Request.Headers["Origin"]);
else
Response.AddHeader("Access-Control-Allow-Origin" , "*"); // Last ditch attempt!
}
}
Run Code Online (Sandbox Code Playgroud)
据我所知,还没有人使用AJAX实现Windows Integrated / NTLM auth,尽管应该这样做(我正在考虑将当前的项目与WindowsTokenRoleProvider结合使用,以进行当前的项目验证)
通过HTTP的NTLM身份验证更多是使用HTTP的CHAP实现,而不是授权的HTTP请求。
如果我真的可以实现这一点,我将向您更新。抱歉,我没有更多帮助。
归档时间: |
|
查看次数: |
21798 次 |
最近记录: |