如何追踪并加载特定用户的信息?
我正在 ASP.net(使用 C#.net4)中创建一个小型门户,用户可以在其中登录并添加、编辑他们的个人信息 (PI)。但我不知道当特定用户登录时如何在页面中加载信息(存储在 SQL Server DB 中)。
例如:如果 Sam 登录,他可以查看他的 PI。当 Vicky 登录时,她可以查看她的 PI。
谁能帮我解决这个问题?
提前致谢。
I'm making a small portal in ASP.net (with C#.net4) where users can login and add, edit their personal information (PI). But I don't get how to load information (stored in a SQL server DB) in the page when a specific user is logged in.
For example: If Sam is logged in, he can view his PI. When Vicky is logged in, she can view her PI.
who can help me with this?
thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要在会话变量中保留登录用户的ID然后使用它来过滤用于获取每个用户信息的查询。
因此,如果用户的 ID 是 278,那么您的查询将运行为:
从存储如下的会话变量:
You need to retain the ID of the logged in user in a session variable and then use it to filter the query with which you fetch each user's info.
So if a user's ID is 278 then your query would run as:
From a session variable stored like:
ASP.NET 会员提供商 已经为您解决了这个问题。您考虑过使用它吗?您可以管理您定义的所有身份验证、权限、角色以及访问/编辑配置文件信息。您可以通过成员资格对象访问数据,并且无需编写一行 SQL 即可完成此操作。它会为您节省大量工作,而不是尝试重新发明轮子。
The ASP.NET membership provider has already taken care of this for you. Have you considered using it? You can manage all of your authentication, permissions, roles, and access/edit profile information -- which you define. You access the data via the membership objects, and you won't need to write a single line of SQL to do it. It will save you loads of work instead of trying to reinvent the wheel.
使用其他答案中所述的常规会员资格。然后利用个人资料系统,以便每个用户在登录时可以查看/编辑他们的信息(根据问题)。注意:ASP.NET 配置文件系统只能与网站项目模板一起使用。如果您想使用 Web 应用程序项目模板,请按照此处的步骤操作:
ASP.NET:网站与 Web 应用程序项目
当配置文件启动并运行时,配置文件数据可以在用户登录时存储在会话对象中。
Use the regular membership as described in the other answers. Then leverage the Profile system so that each user can view/edit their info when logged in (per the question). CAVEAT: ASP.NET profile system only works out of the box with the Website project template. If you want to use the Web Application project template, then follow the steps here:
ASP.NET: Web Site versus Web Application Project
When you have the profiles up and running, the profile data can be stored in session objects while the user is logged in.