【发布时间】:2014-06-16 16:22:01
【问题描述】:
为什么网络安全在不同浏览器上的工作方式不同:
详情:
我有两个应用程序
一个是简单的HTML 应用程序,另一个是ASP.NET MVC4 WebApi 应用程序,项目在同一个解决方案中,我设置了多个启动项目以同时运行应用程序。
工作版本:
我在 Web API 项目中使用了 Web Security。我已经全面实施了网络安全......
登录操作代码
// GET api/company
[System.Web.Http.AcceptVerbs("Post")]
[System.Web.Http.HttpPost]
public HttpResponseMessage Login(LoginRequest loginRequest)
{
try
{
if (WebSecurity.Login(loginRequest.EmailAddress, loginRequest.Password, true))
{
var userDetails = new string[2];
userDetails[0] = loginRequest.EmailAddress;
var currentUSerRole = Roles.GetRolesForUser(loginRequest.EmailAddress);
userDetails[1] = currentUSerRole[0].ToString();
HttpResponseMessage response =
Request.CreateResponse(HttpStatusCode.Accepted, userDetails);
return response;
}
else
{
HttpResponseMessage response
= Request.CreateResponse(HttpStatusCode.Unauthorized);
return response;
}
}
catch (Exception e)
{
HttpResponseMessage response
= Request.CreateResponse(HttpStatusCode.Unauthorized);
return response;
}
}
当我使用Ajax 调用登录方法时,*WebSecurity.Login* 正在所有浏览器上工作。
但我在另一个控制器中有另一种方法,名为CurrentDateAndUser
代码:
[AllowAnonymous]
[System.Web.Http.AcceptVerbs("Get")]
[System.Web.Http.HttpGet]
public HttpResponseMessage CurrentDateAndUser()
{
if (WebSecurity.IsAuthenticated)
{
int userId = WebSecurity.CurrentUserId;
string[] currentDateAndUSerId = new string[2];
currentDateAndUSerId[0] = userId.ToString();
currentDateAndUSerId[1] = DateTime.UtcNow.ToString();
HttpResponseMessage response =
Request.CreateResponse(HttpStatusCode.Accepted, currentDateAndUSerId);
return response;
}
HttpResponseMessage responseNew =
Request.CreateResponse(HttpStatusCode.NotAcceptable);
return responseNew;
}
问题:
- 如果我使用 Ajax 调用从 Microsoft Internet Explorer 调用
CurrentDateAndUser方法,那么一切正常。WebSecurity.IsAuthenticated返回 true 并且运行良好。
然而,
- 如果我使用 Ajax 调用从 Google Chrome 或 Mozilla Firefox 调用
CurrentDateAndUser方法,则没有任何效果。WebSecurity.IsAuthenticated总是返回 false。
我不知道为什么。如果您有任何想法,请告诉我。
我也发现了一个类似的问题(不确定是不是真的问题):
当我使用 Fiddler 运行我的应用程序时,我看到了不同的结果:
当我从 IE 调用 CurrentDateAndUser 方法时,请求是:
我可以在上图中看到 Cooke/Login 值
但是当我从 Chrome 和 Firefox 调用 CurrentDateAndUser 方法时,请求是:
我看不到 cookie 值,这意味着 Web Security.IsAuthenticated 属性正在返回 false。
是WebSecurity中的Bug??????
编辑
我的 Ajax 请求代码是
function GetCurrentUserId() {
return $.ajax({
method: 'GET',
url: rootUrl + '/api/Common/CurrentDateAndUser',
async: false
}).success(function (response) {
return response[0];
}).error(function () {
toastr.error('Somthing is wrong', 'Error');
})
}
当我在 Chrome 和 Firefox 中运行应用程序时,此请求不会将 Auth Cookie 值发送到 Web API 方法,但是,如果在 IE 中运行,此请求会将 cookie 值发送到 API 方法 em>
我已经发布了图片,请看上面的图片
【问题讨论】:
-
可能你只需要在第二种情况下发送 HttpStatusCode.Unauthorized 以使浏览器再次登录。
-
我也试过了。这是没有用的。我想我有任何 cors 问题,但我还没有得到解决方案。 f**King 问题..
-
用 fiddler 调用你的应用程序,看看在 firefox 和 chrome 上会发生什么。你应该看到一个 401 代码。如果您收到 401 并且没有提示输入用户名,如果代码与 401 不同,则它是浏览器上的一个错误,您将覆盖响应。通常你应该看到两个 401,然后是 2** 或 3** 响应。
-
PS:我假设您将 CurrentDateAndUser 更改为使用 HttpStatusCode.Unauthorized 进行错误回复
-
为什么端口号不同(50949 vs 12345)?并查看此问题的答案(阅读他们对 IE 行为的看法):stackoverflow.com/questions/1612177/…
标签: c# javascript asp.net asp.net-mvc asp.net-web-api