繁体   English   中英

IE VS Chrome和Firefox中的网络安全(bug)

[英]Web Security in IE VS Chrome & Firefox (bug)

为什么Web Security在不同的浏览器上的工作方式不同:

细节:

我有两个申请

一个是简单的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调用login方法时, *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;
}

问题:

  • 如果我从Microsoft Internet Explorer使用Ajax调用调用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方法

我发布了图片,请看一下上面的图片

问题根本不在于网络安全,而在于您实施安全性的方式。 您永远不应该使用用户ID,电子邮件或cookie中的任何重要内容。

我建议您使用FormsAuthentication类来加密和解密您的cookie,即便如此,只存储诸如SessionID之类的内容以及该会话ID的自定义哈希值,以便在您解密cookie时验证自己的身份

这是一个很好的例子: http//www.c-sharpcorner.com/uploadfile/nipuntomar/update-formsauthenticationticket/

它周围有三件事:

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);

我不知道这是否有帮助。

但我记得我正在学习jQuery ajax所以我在笔记本电脑上设置了一个简单的项目。 当我测试它时,它在IE上工作正常,但在Chrome中失败了。 搜索了几个小时后,我发现Chrome不允许来自本地计算机的AJAX请求。 当我使用实际的Web服务器测试它时,它适用于IE和Chrome。

所以我的问题和建议是:你在同一台机器上测试吗? 尝试将其部署到运行具有唯一域名的Web服务器的计算机上并测试您的应用程序!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM