简体   繁体   English

如何防止同一用户在不同电脑上同时登录

[英]How to prevent simultaneous login with same user on different pcs

We have build a intranet application where users have to login to do certain tasks... We have to make sure that no "application user" is logged in more than once at the same time.我们已经构建了一个 Intranet 应用程序,用户必须在其中登录才能执行某些任务……我们必须确保没有“应用程序用户”同时登录多次。

So what I do at the moment is that I store the current asp .net session id in the database and then i compare at every page load wheter they are same or not.所以我现在所做的是将当前的 asp .net 会话 ID 存储在数据库中,然后在每个页面加载时进行比较,无论它们是否相同。 The session id is stored in the database when the user logs in.当用户登录时,会话 ID 存储在数据库中。

But by using this kind check, there is always a database select needed.但是通过使用这种检查,总是需要一个数据库选择。 So I don't like this way very much.所以我不太喜欢这种方式。 There must be a more elegant way to solve this, or?必须有更优雅的方法来解决这个问题,或者?

We use ASP .Net2, C#..我们使用 ASP .Net2, C#..

Thanks in advance for any input提前感谢您的任何意见

[Info Update] [信息更新]

I have already created a custom Membershipprovider and a custom Membershippuser.我已经创建了一个自定义 Membershipprovider 和一个自定义 Membershippuser。 The Membershipuser has a method called "StartSession(string sessionId)" which is used, when the user logs in. Membershipuser 有一个名为“StartSession(string sessionId)”的方法,在用户登录时使用。

The other method CheckSession(string sessionId) is used at every postback, and it compares the current session id with the session id stored in the database.另一个方法 CheckSession(string sessionId) 在每次回发时使用,它将当前会话 ID 与存储在数据库中的会话 ID 进行比较。

[Update] Thanks everybody for your input. [更新]感谢大家的意见。 I will now use the cache to prevent permanent database access.我现在将使用缓存来防止永久数据库访问。 I first thought that there is already a Class or something that is already handling this problem.我首先想到的是已经有一个类或已经在处理这个问题的东西了。

Your existing approach of storing this info in the DB is reasonable, it helps if things scale up.您现有的将此信息存储在数据库中的方法是合理的,如果事情扩大,它会有所帮助。

However you could also use the System.Web.Caching.Cache object to track a users current session as well.但是,您也可以使用 System.Web.Caching.Cache 对象来跟踪用户当前的会话。 If finding the info in the cache object fails fall back to reading it from the DB then place that info in the cache for the benefit of subsequent requests.如果在缓存对象中找到信息失败,则回退到从数据库读取它,然后将该信息放入缓存中以供后续请求使用。

Whilst digging around for something related to this earlier today I ran across this article that may be of use:今天早些时候在挖掘与此相关的内容时,我偶然发现了这篇可能有用的文章:

Preventing Multiple Logins in ASP.NET (EggHead Cafe)在 ASP.NET 中防止多次登录(EggHead Cafe)

The main change I would suggest is that you create a session cache to check against, rather then using the database on every page load.我建议的主要更改是您创建一个会话缓存来检查,而不是在每个页面加载时使用数据库。

It would work in a similar way - you would check the session id in the cache to validate and take the same action if validation fails.它会以类似的方式工作 - 您将检查缓存中的会话 ID 以进行验证并在验证失败时采取相同的操作。 You just wouldn't need to make the database calls.您只是不需要进行数据库调用。

As the other answers state, caching will give you a peformance boost here, but do check if you actually require this for an intranet app.正如其他答案所述,缓存将在此处为您提供性能提升,但请检查您是否真的需要为 Intranet 应用程序提供此功能。

What you are describing breaks the standard model for web applications, and I would question the value of this for purely licensing reasons.您所描述的内容打破了 Web 应用程序的标准模型,出于纯粹的许可原因,我会质疑其价值。 Particularly if you intend to try and prevent people opening multiple tabs - you are likely to embarking on a very difficult task, which will only reduce the quality of your user experience.特别是如果您打算尝试阻止人们打开多个选项卡 - 您可能会开始一项非常艰巨的任务,这只会降低您的用户体验质量。

Well, I know, this is very old post, But I recently solved this issue, by using, formsauthentication ticket, I am checking and maintaining a static dictionary of all loggged in users, with their login timestamps from formsauth ticket.嗯,我知道,这是一篇很老的帖子,但我最近解决了这个问题,通过使用表单验证票,我正在检查和维护所有登录用户的静态字典,以及他们来自 Formsauth 票的登录时间戳。 When a second user logs in on different machine, he updates his timestamp in this dictionary, Now when first user will try to validate his FormsAuth timestamp, he will find a different timestamp in the dictionary and he will log off his session, In this way, only newly logged in user will stay alive..当第二个用户在不同的机器上登录时,他会在这个字典中更新他的时间戳,现在当第一个用户尝试验证他的 FormsAuth 时间戳时,他会在字典中找到一个不同的时间戳,然后他将注销他的会话,以这种方式,只有新登录的用户才能存活。

Make a database fro user login with an additonal field Active or something similar.使用附加字段 Active 或类似字段为用户登录创建数据库。 On user authentication check for the value in this field from the user table if it is already true display message through javascript "User already Logged in" if the value of Active is false , update and set it to true and authenticate user to next page or form.在用户身份验证中检查用户表中此字段中的值,如果它已经为真如果 Active 的值为假,则通过 javascript“用户已经登录”显示消息,更新并将其设置为真,然后将用户身份验证到下一页或形式。 Check this on page_load event of page next in navigation after user authentication.在用户身份验证后,在导航中的页面 next 的 page_load 事件上检查此项。 if user is already login then the page controls can be made read only else, records can be changed on the page.如果用户已经登录,则可以将页面控件设为只读,否则可以更改页面上的记录。

I used this and it worked.我使用了这个并且它起作用了。 there are no session out in my application.我的应用程序中没有会话。

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

相关问题 如何防止用户从多个位置或同一台PC上的不同浏览器登录 - How to prevent user to login from multiple location or from different browser in same pc 不同PC上具有相同分辨率的不同字体大小 - Different font sizes on different PCs with same resolutions 如何防止使用缓存在asp.net中从同一用户ID登录多个用户? - How to prevent multiple user login from the same user id in asp.net using cache? 防止用户组中的同时并发和最大登录 - Prevent simultaneous concurrent & maximum logins in User group 如何在应用程序和Windows服务之间共享相同的设置(或配置)(即使它们在不同的PC上运行) - How to share the same settings (or configuration) between apps and windows services (even if they run in different PCs) 防止在不同浏览器中使用一个用户登录 .NET Core - Prevent login with one user in different browser .NET Core 如何避免同一用户多次登录 - How to avoid multiple login for the same user 如何将两台不同的PC连接到mysql数据库c# - How to connect two different PCs to mysql database c# 如何防止单个用户名多次登录 - How to prevent multiple login from single user name 如何防止客户端在用户登录之前查看 WebResource.axd? - How to prevent client to view WebResource.axd until user login?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM