如何在ASP.NET MVC 5中验证未授权的用户

问题描述 投票:0回答:1

不幸的是我找不到接近我的答案,所以我希望有人可以给我一个建议。

我可以带用户ip,但它可以是动态的,所以这不是100%的保证,而且在一个ip上可以有几个用户。

我也可以使用anonymousIdentification

<anonymousIdentification
        enabled="true"
        cookieless="UseCookies"
        cookieName=".ASPXANONYMOUS"
        cookieTimeout="30"
        cookiePath="/"
        cookieRequireSSL="false"
        cookieSlidingExpiration = "true"
        cookieProtection="Validation"
      />

但它基于可以轻松关闭或清除的cookie。

我知道有人使用evercoockie,但我觉得它太粗糙了,也许我错了。

所以我想知道是否有可能更可靠地识别用户的计算机和移动设备。

c# asp.net asp.net-mvc
1个回答
0
投票

您可以为每个guest虚拟机生成GUID并将其保存在cookie中。因此,每次用户访问它都会检查cookie。然后,您可以将GUID用作其他实体的标识符,而不是用户ID。我已将此系统用于允许访客和用户使用服务的页面。它必须是GUID,而不是int,因此无法手动操作cookie。

    public Guid InitGuestCookie()
    {
        if (Request.Cookies.AllKeys.Contains(Utils.Constants.GuestId))
        {
            var id = Request.Cookies[Utils.Constants.GuestId];

            if (id != null)
            {
                var guid = new Guid(id.Value);
                // Make sure this guid exists as a registered guest
                var guest = _guestService.GetById(guid);
                if (guest != null)
                {
                    return guest.Id;
                }
            }
        }

        var newGuest = CreateGuest();
        var cookie = new HttpCookie(Utils.Constants.GuestId, newGuest.Id.ToString());
        Response.Cookies.Set(cookie);
        return newGuest.Id;
    }

    public Guid GuestId { get; set; }

    public Guest CreateGuest()
    {
        var guid = Guid.NewGuid();
        var newGuest = new Guest()
        {
            CreateDate = DateTime.Now,
            LastUsageDate = DateTime.Now,
            Id = guid,
        };

        _guestService.AddOrUpdate(newGuest);
        return newGuest;
    }

在控制器的Init方法中我调用:

    protected override void Initialize(RequestContext requestContext)
    {
        base.Initialize(requestContext);
        GuestId = InitGuestCookie();
    }
© www.soinside.com 2019 - 2024. All rights reserved.