我有一个使用Windows身份验证的MVC 4应用程序。我目前的挑战是我需要为用户提供注销按钮。我可以直接在Chrome中实现这一点,但是当我将谷歌Chrome框架(GCF)添加到组合中时,我似乎有问题。
我正在使用IE9和GCF。
当我尝试发送AJAX请求时,这就是GCF网络跟踪告诉我的内容:
请求:
GET http://103581:107674@linkToMySite/LogOff401?_=1369312905224 HTTP/1.1
Accept: */*
Referer: http://LinkToMySite
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36
Authorization: user:pass
不幸的是,响应失败了。 Chrome开发者工具会把它拿起来,但我认为IE正在阻止请求离开,因为Fiddler没有看到它。我很茫然,并且已经对此进行了多次故障排除,不确定这是否应该向Google报告为错误。
这是客户端注销代码(来自此处http://trac-hacks.org/wiki/TrueHttpLogoutPatch的改编):
function LogMeOut(url) {
try {
document.execCommand("ClearAuthenticationCache");
SendAJAXLogout(url);
}
catch(err){
try {
SendAJAXLogout(url);
} catch(err) {
log("Unable to log this user out.");
}
}
window.location.href = url;
}
function SendAJAXLogout(url) {
$.ajax(url, {
username: GetRandomNumber(10000, 99999),
password: GetRandomNumber(10000, 99999),
beforeSend: function (req) {
req.setRequestHeader('Authorization', "user:pass");
},
cache: false,
error: function (jqXHR, textStatus, errorThrown) {
//alert("Error: " + textStatus);
log("I'm logged out now");
}
});
}
这是我在其他地方找到的服务器端操作方法,但找不到链接(我没写这个):
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult LogOff401(string id)
{
// if we've been passed HTTP authorisation
string httpAuth = this.Request.Headers["Authorization"];
if (!string.IsNullOrEmpty(httpAuth) &&
httpAuth.StartsWith("basic", StringComparison.OrdinalIgnoreCase))
{
// build the string we expect - don't allow regular users to pass
byte[] enc = Encoding.UTF8.GetBytes(id + ':' + id);
string expected = "basic " + Convert.ToBase64String(enc);
if (string.Equals(httpAuth, expected, StringComparison.OrdinalIgnoreCase))
{
return Content("You are logged out.");
}
}
// return a request for an HTTP basic auth token, this will cause XmlHttp to pass the new header
this.Response.StatusCode = 401;
this.Response.StatusDescription = "Unauthorized";
this.Response.AppendHeader("WWW-Authenticate", "basic realm=\"My Realm\"");
return Content("Force AJAX component to sent header");
}