如何在C#中创建登录会话? [英] How do I create a login session in C#?

查看:91
本文介绍了如何在C#中创建登录会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我正在创建一个用户登录然后开始工作的应用程序。我想要的是,一旦用户登录,它将创建一个会话,其中用户保存的数据保留在该特定会话中。



我有登录创建的表单从SQL Server获取其数据,但现在我想连接这两个表单并创建一个会话。登录后,它应显示欢迎*用户名*



请帮忙。



< b>我尝试了什么:



我不知道如何在C#WinForms中创建会话。

Hello,

I am creating an application where the user will login and then start working. What I want is that once the user has logged in, it will create a session where the data saved by the user stays in that particular session.

I have a login form created that fetches its data from SQL Server but now I want to connect the two forms and create a session. So after logging in, it should show "Welcome *Username*"

Please help.

What I have tried:

I do not know how to create a session in C# WinForms.

推荐答案

在WinForms中没有会话这样的东西:它是网站中需要的构造,因为您可能有多个用户同时登录,并且单个应用程序(IIS)需要注意他们所有人中。每个用户都获得一个Session,这样他的数据就可以与其他用户保持不同,而不需要为每个单独的用户运行完整的应用程序实例的开销。



随着Winforms,它是不同的:通常每台机器只有一个用户,应用程序只运行一个用户:如果同一台机器上需要两个用户,则运行该应用程序的第二个实例。你不需要一个会话,因为应用程序中没有其他用户可以混淆东西!



所以你的过程是:



开始使用应用程序。

打开登录表单。

验证用户。

隐藏或关闭登录表单,打开主表单,将用户详细信息传递给主表单。

主表单然后将用户信息存储为表单实例的一部分,并显示欢迎消息。
There is no such thing as a "session" in WinForms: it's a construct that is needed in websites because you may have multiple users logged in at the same time, and a single application (IIS) takes care of them all. Each user gets a Session so that his data can be kept distinct from all the others, without needing the overhead of a complete application instance running for each separate user.

With Winforms, it's different: normally there is only one user per machine, and the application runs for just that one user: if you need two users on the same machine at the same time, you run a second instance of the application. You don't need a "session" because there is no other user in the application to confuse things with!

So your process is:

Start app.
Open login form.
Verify user.
Hide or close login form, open main form, pass user details to main form.
The main form then stores the user info as part of it's form instance, and displays your "welcome" message.


如果我正确理解了这个问题,一种简单的方法是使用静态类来存储应用程序范围的信息。例如



If I understand the question correctly, one simple way is to use a static class to store application wide information. For example

public static class MyAppData {
   public bool LoggedIn { get; set; }
   public string UserName { get; set; }
}



你可以简单地使用例如


You can access the data simply using for example

if (!MyAppData.LoggedIn) {
   ...
}
...
MessageBox.Show("


欢迎{MyAppData.UserName} );


这篇关于如何在C#中创建登录会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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