0

我有一个登录页面,用户第一次登录该页面时可以使用任何机器。一旦用户第一次登录,我需要限制该用户不登录另一台机器。所以用户只需要使用一台第一次登录的机器。

我试图获取客户端的 MAC 地址,但我无法在我的网站中获取客户端的 MAC 地址。有没有其他方法可以唯一地识别机器?

4

3 回答 3

1

对于 asp.net,无法获取客户端的 mac 地址。为此,您需要有某种在用户系统上运行的 Windows 应用程序。

带有 GUID 的永久 cookie 也可能是一种解决方案。

另一种解决方案可能是在他们发出请求时查找服务器变量,您将拥有 Request.ServerVariables["REMOTE_ADDR"]; 如果应用程序是内部/内部网,这可能是内部 IP。还有REMOTE_HOST。有时这些会被代理/防火墙/nat 过滤掉,但希望不会出现在您的情况下。

希望能帮助到你!

于 2013-11-06T09:07:30.807 回答
1

如果它的 Intranet webapp,那么您可以强制执行 Windows 身份验证 - 并在数据库中保留登录用户列表,其中包含登录用户在时间戳期间后自动注销的时间戳。

或者,在表单身份验证中使用 cookie 来做到这一点。但无论如何,您都需要登录用户列表,如果用户在另一台机器上,则自动注销该用户。

更重要的是,您可以获取客户端的 IP 地址并从那里开始,但它不可靠,因为它可能来自 ISP。这很棘手,但 cookie 似乎是最简单的方法。

然而,一个好的解决方案是像 IRC 那样做,以跟踪登录用户。它向客户端发送 PING,并期望客户端以不同的时间间隔返回 PONG。如果客户端没有收到 PONG,IRC 服务器会自动断开用户的连接。用 SignalR 之类的东西试试这个。这样做的缺点是,如果用户关闭浏览器并且 PING 请求进来,它将反弹,客户端将断开连接,因为他/她将无法发送 PONG 请求。

于 2013-11-06T09:12:19.057 回答
0

我相信您希望用户在任何给定时间仅在一个会话中登录网站。问题是你不能确定用户何时离开,如果他没有使用注销按钮注销。要解决这个问题,你必须有一个超时。我在应用程序的服务器上使用了一个文本文件,它可以工作。

登录按钮:

    protected void btLogin_Click(object sender, EventArgs e)
    {
        if (check(txtPass.Text) && check(txtUser.Text))
        {
            var user = new UserManager().login(txtUser.Text, txtPass.Text);
            if (user != null)
            {
                // this is the test you're looking for, the rest is only context
                if (!FileManager.alreadyLoggedIn(user.email))
                {
                    FormsAuthentication.SetAuthCookie(user.email, false);
                }
                else
                {
                    //throw error that it is already connected in some other place
                }
            }
            else
            {
                    //throw error that login details are not OK
            }
        }
    }

在一个类中,有两个静态方法:

    //you have to call this function at every request a user makes
    internal static void saveUserSessionID(string email)//email or any unique string to user
    {
        var dir = HostingEnvironment.MapPath("~/temp/UserSession/");// a folder you choose
        if (!Directory.Exists(dir))
        {
            Directory.CreateDirectory(dir);
        }
        string path = dir + email + ".txt";
        File.WriteAllText(path, HttpContext.Current.Session.SessionID);
    }

    // if a request has not been made in tha last 4 minutes, the user left, closed the browser
    // the test checks this only on a real server, localhost is not tested to be easy for the developer
    internal static bool alreadyLoggedIn(string email)
    {
        var file = HostingEnvironment.MapPath("~/temp/UserSession/" + email + ".txt");
        return File.Exists(file) && File.GetLastWriteTime(file).AddMinutes(4) > DateTime.Now && !HttpContext.Current.Request.IsLocal;
    }

显然这是来自另一个应用程序,您只能采用这个想法并在您自己的应用程序中实现它。你不能只是复制粘贴它。

于 2013-11-06T09:35:50.197 回答