详细信息:
我有两个应用程序
一个是简单的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;
}
}
当我使用*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;
}
问题:
CurrentDateAndUser
方法使用Ajax调用,那么一切正常。 WebSecurity.IsAuthenticated
返回true并且运行良好。然而,
CurrentDateAndUser
方法,则无效。 WebSecurity.IsAuthenticated
始终返回false。我不知道为什么。如果您有任何想法,请告诉我。
我也发现了一个类似的问题(不确定这是否是一个真正的问题):
当我用Fiddler运行我的应用程序时,我看到了不同的结果:
当我从IE调用CurrentDateAndUser
方法时,请求为:
我可以在上面的图片中看到Cooke / Login值
但是当我从Chrome和Firefox调用CurrentDateAndUser
方法时,请求是:
我无法看到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');
})
}
当我在Chrome和Firefox中运行应用程序时,此请求不会将Auth Cookie值发送到Web API方法,但是,此请求会将Cookie值发送到API方法(如果它在IE中运行)
我发布了图片,请看一下上面的图片
答案 0 :(得分:2)
问题不在于网络安全,而在于您实施安全性的方式。您永远不应该使用用户ID,电子邮件或cookie中的任何重要内容。
我建议您使用FormsAuthentication
类来加密和解密您的cookie,即便如此,只会存储诸如SessionID之类的内容以及该会话ID的自定义哈希值,以便在您解密cookie时验证自己的身份
这是一个提供相当不错的示例的网站:http://www.c-sharpcorner.com/uploadfile/nipuntomar/update-formsauthenticationticket/
答案 1 :(得分:0)
它周围有三件事:
WebSecurity.IsAuthenticated实际上返回HttpRequest.IsAuthenticated的值,如果已经设置了Forms Authentication cookie并且它是最新的,则为true。在用户成功登录后发出下一个请求之前,它不可用,这就是您看到所描述的行为的原因。
我记得在MSDN或某个地方阅读,WebSecurity.IsAuthenticated在页面完全加载之前不起作用。这意味着如果您在页面中登录用户并且在相同的代码流中检查IsAuthenticated,则不会返回True。对于IsAuthenticated为True,必须重新加载页面或使用更好的练习;这是在登录成功后立即将用户重定向到另一个安全页面,并在该页面中检查IsAuthenticated。
我们在使用Chrome时遇到了同样的问题(版本21.0.1180)。尽管我们看到Header上的截止日期,但Windows XP中的某些Chrome会忽略它。然后我们删除了截止日期,Chrome接受了保持会话cookie没有问题。
那么该做的是: 登录后尝试在不在同一页面上的新页面上进行检查。
还尝试明确设置cookie
System.Web.Security.FormsAuthentication.SetAuthCookie(user.Username, false);
答案 2 :(得分:0)
我不知道这是否有帮助。
但我记得我正在学习jQuery ajax 所以我在笔记本电脑上设置了一个简单的项目当我测试它时,它在IE上工作正常,但在Chrome中失败了。搜索了几个小时后,我发现Chrome不允许来自本地计算机的AJAX请求。当我使用实际的Web服务器测试它时,它适用于IE和Chrome。
所以我的问题和建议是:你在同一台机器上进行测试吗? 尝试将其部署到运行具有唯一域名的Web服务器的计算机上并测试您的应用程序!