14 javascript c# asp.net asp.net-mvc asp.net-web-api
细节:
我有两个申请
一个是简单的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;
}
}
Run Code Online (Sandbox Code Playgroud)
*WebSecurity.Login*当我使用登录方法调用时,我正在使用所有浏览器Ajax.但我在另一个控制器中有另一种方法,名为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;
}
Run Code Online (Sandbox Code Playgroud)
问题:
CurrentDateAndUser从Microsoft Internet Explorer使用Ajax调用调用该方法,那么一切正常.在WebSecurity.IsAuthenticated返回true,并且运作良好.但是,
CurrentDateAndUser使用Ajax调用从Google Chrome或Mozilla Firefox 调用该方法,则无效.将WebSecurity.IsAuthenticated始终返回false.我不知道为什么.如果您有任何想法,请告诉我.
我也发现了一个类似的问题(不确定它是否是一个真正的问题):
当我用Fiddler运行我的应用程序时,我看到了不同的结果:
当我CurrentDateAndUser从IE 调用该方法时,请求是:

我可以在上面的图像中看到Cooke/Login值
但是当我CurrentDateAndUser从Chrome和Firefox 调用该方法时,请求是:

我看不到cookie值,这意味着该Web Security.IsAuthenticated属性正在返回false.
它是错误的WebSecurity?????
我的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');
})
}
Run Code Online (Sandbox Code Playgroud)
当我在Chrome和Firefox中运行应用程序时,此请求不会将Auth Cookie值发送到Web API方法,但是,如果在IE中运行,此请求会将cookie值发送到API方法
我发布了图片,请看一下上面的图片
围绕它有3件事:
WebSecurity.IsAuthenticated 实际上返回 HttpRequest.IsAuthenticated 的值,如果表单身份验证 cookie 已设置并且是最新的,则该值为 true。在用户成功登录后发出下一个请求之前,它不可用,这就是您看到所描述的行为的原因。
我记得在 MSDN 或其他地方读过,WebSecurity.IsAuthenticated 在页面完全加载之前不起作用。这意味着如果您在页面中登录用户并在检查 IsAuthenticated 的同一代码流中,它将不会返回 True。要使 IsAuthenticated 为 True,必须重新加载页面或使用更好的做法;这是在登录成功后立即将用户重定向到另一个安全页面,并在该页面中检查 IsAuthenticated。
我们在 Chrome(版本 21.0.1180)上遇到了同样的问题。尽管我们在标头上看到了过期日期,但 Windows XP 中的某些 Chrome 忽略了它。然后我们删除了过期日期,Chrome 接受了保留会话 cookie,没有任何问题。
所以要做的是:登录后尝试在新页面而不是同一页面上检查此内容。
还尝试显式设置 cookie
System.Web.Security.FormsAuthentication.SetAuthCookie(user.Username, false);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1228 次 |
| 最近记录: |