c# - IE VS Chrome 和 Firefox 中的网络安全(错误)

标签 c# javascript asp.net asp.net-mvc asp.net-web-api

为什么 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;
    }  
}
当我使用 Ajax 调用登录方法时,

*WebSecurity.Login* 适用于所有浏览器。 但是我在另一个 Controller 中有另一种方法,名为 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 方法时,请求是:

enter image description here

我可以在上图中看到 Cooke/Login 值


但是当我从 Chrome 和 Firefox 调用 CurrentDateAndUser 方法时,请求是:

enter image description here

我看不到 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 方法

我已经发图了,请看上面的图

最佳答案

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

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

这里有一个网站提供了一个很好的例子:http://www.c-sharpcorner.com/uploadfile/nipuntomar/update-formsauthenticationticket/

关于c# - IE VS Chrome 和 Firefox 中的网络安全(错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23386532/

相关文章:

c# - 正则表达式的动态解释

javascript - 可观察对象存储在浏览器内存中的什么位置?

asp.net - MasterPage 和 ContentPage Body Css 问题?

c# - 在 GridView 列中拟合长文本

c# - StringBuilder.Append 与 StringBuilder.AppendFormat

c# - 在我的事件处理程序范围内遇到问题(ASPX .NET 3.5)

c# - linq to sql左连接,需要检查右表是否为空

javascript - nightmare.js 如何实现类似window.callPhantom() 函数的功能?

javascript - 微前端/Web 组件/Vue 路由器错误 : Uncaught TypeError: Cannot redefine property: $router

c# - 从客户端正则表达式实现服务器端正则表达式验证