在 Webforms ASP.net 上为每个用户创建唯一的会话 [英] Creating unique sessions per user on Webforms ASP.net

查看:60
本文介绍了在 Webforms ASP.net 上为每个用户创建唯一的会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们目前正在 ASP.net 上开发一个简单的系统,供用户同时使用.我想要发生的是每次登录系统时从 SQL 表中获取每个用户的 Name(或任何其他有用的信息)并在整个过程中使用这些数据(注意:这些数据应该每个用户都是唯一的).

现在我的问题是,在这种情况下为每个用户提供唯一变量/会话的正确方法是什么?如果我想让这个变量成为一个全局变量怎么办?

示例场景:一位医生登录系统,后面的代码从表格中获取他的名字,并在首页提示数据——欢迎约翰医生!"(假设他的名字是John医生.另外一个用户登录系统,分别得到他的名字和提示,现在如果第一个用户John刷新页面就会出现冲突,这是我想避免的冲突.

我可以阅读有关此事的任何文章吗?任何帮助将不胜感激.

免责声明:当谈到 ASP.net 时,我仍然是初学者,所以我为这么简单的问题道歉.

解决方案

Session 是您保存这些信息的地方.它将对每个用户都是唯一的.在以下位置阅读更多信息:ASP.NET Session状态概览

请记住,会话是在服务器上为每个用户维护的.它们很昂贵.因此,如果您在会话中保留了太多数据,那么您最终可能会在服务器上占用更多资源.

考虑以下示例,您从数据库中检索对象中的 UserInfo.

UserInfo userInfo = GetUserInfoFromDB();

在会话中存储信息:

//一旦用户通过身份验证//存储会话会话[用户信息"] = 用户信息;

检索信息:

UserInfo currentUserInfo = Session["UserInfo"] as UserInfo;if(currentUserInfo != null){//找到信息//分配标签文本 currentUserInfo.UserName 等}

您可能会看到:探索 ASP.NET 中的会话 - 代码项目

We are currently developing a simple system on ASP.net which are going to be used by users simultaneously. What I want to happen is to get each users' Name (or any other useful info) from a SQL table every time they log in to the system and use this data all throughout (Note: this data should be unique per user).

Now my question is, what is the proper approach on this kind of scenario to give unique variable/session per user? And what if I want to make this variable a global one?

Sample Scenario: A doctor logs in to the system and the code behind gets his name from the table and prompts the data on the homepage - "Welcome Doctor John!" (assuming his name is doctor John. Another user logs in to the system, gets his name and prompts respectively, now the conflict arises if the first user - John, refreshes the page and this is the conflict that I want to avoid.

Any article that I could read on with regards the matter? Any help would be much appreciated.

Disclaimer: I am still a beginner when it comes to ASP.net so my apologies for such simple question.

解决方案

Session is where you will keep this information. It will be unique for each user. Read more about it at: ASP.NET Session State Overview

Just remember, that Sessions are maintained on Server for each user. They are costly. So if you keep too much data in your session then you may end up claiming more resources on the server.

Consider the following example where you retrieve your UserInfo in an object from Database.

UserInfo userInfo = GetUserInfoFromDB();

To Store information in Session:

//once user is authenticated
//store session
Session["UserInfo"] = userInfo;

To Retrieve information:

UserInfo currentUserInfo = Session["UserInfo"] as UserInfo;
if(currentUserInfo != null)
{
    //info found
    // assign lable Text currentUserInfo.UserName etc
}

You may see: Exploring Session in ASP.NET - Code Project

这篇关于在 Webforms ASP.net 上为每个用户创建唯一的会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆