简体   繁体   English

像asp.net中的会话一样,如何使用c#登录Windows应用程序?

[英]How to login windows application using c# just like sessions in asp.net?

I am developing an web application, so now i have a requirement to develop a login page so in my app nearly 20 forms and 1 master page. 我正在开发一个Web应用程序,所以现在我需要开发一个登录页面,因此在我的应用程序中有将近20种表单和1个母版页。

So i designed 1 login form i stored the details in sql db and i checked all the credentials everything is working. 所以我设计了1个登录表单,将详细信息存储在sql db中,然后检查了所有凭据,一切正常。

But when the user login from login page i showed the master form in that nearly 20 forms are there with linklabel. 但是,当用户从登录页面登录时,我显示了主表单,其中有将近20个带有linklabel的表单。 So when the master page is opened i showed his name on top of the master page. 因此,当打开母版页时,我在母版页顶部显示了他的名字。 It is fine. 没事。 I showed this name 我叫这个名字

Login form: 登录表单:

  master MasterObj = new master();

  MasterObj.values = this;

  MasterObj.Show();

In master form: 以主形式:

  string login values;

   label1.text=values.txtusername.text; 

So i successfully dispalyed message on this master form. 因此,我成功地在此主表单上分发了消息。

So the subforms also there is linklabel to redirect master form, then there is a problem to dispaly name "object reference cannot instance of an object". 因此,子表单也有linklabel来重定向主表单,然后出现了一个问题,即显示名称“对象引用不能为对象的实例”。 i know why this error came? 我知道为什么会出现此错误? beacause i am not intialize the name value from the subform. 因为我没有初始化子窗体中的名称值。

Which is the best way to get then name of the user to all the win forms in my application thanks. 这是在我的应用程序中所有赢得表单中获得用户名的最佳方法,谢谢。

You can create a Loginclass that handles the Login User ID , and once logged you can set the value 您可以创建一个处理登录用户ID的Loginclass,一旦登录,就可以设置该值

 public static LoggedUser{
    public Static string userid;
    }

in each form on load you can check the userid if not empty . 在每种加载表格中,您可以检查用户ID是否不为空。

or you can get benefit from the settings property 或者您可以从settings属性中受益

    Settings.Default.UserID = UserID.Text;
        Settings.Default.Save();

You can create a static variable in Program.cs file and then you can access it from through out the application like 您可以在Program.cs文件中创建一个静态变量,然后可以像这样在整个应用程序中进行访问

declare the variable in program .cs file as 在程序.cs文件中将变量声明为

public static string LoggedUser=""; 公共静态字符串LoggedUser =“”;

assign the username to it like 像分配用户名

Program.LoggedUser=txtUserName.Text; Program.LoggedUser = txtUserName.Text;

And Accessing it through out the application is like 通过应用程序访问它就像

string UserName=Program.LoggedUser.ToString(); 字符串UserName = Program.LoggedUser.ToString();

暂无
暂无

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

相关问题 使用ASP.NET C#中的会话验证登录级别 - authenticating login level using sessions in asp.net c# 如何在C#Windows应用程序中动态添加datagridview中的行,就像不使用数据库在Asp.net Web应用程序中添加到购物车一样 - How to add rows in datagridview dynamically in C# windows application just like Add to Cart in Asp.net Web application without using database 如何在C#Windows应用程序中动态更新datagridview中的行,就像不使用数据库在Asp.net Web应用程序中添加到购物车一样 - How to update rows in datagridview dynamically in C# windows application just like Add to Cart in Asp.net Web application without using database 在C#应用程序中的ASP.NET登录 - Asp.Net login in c# application 如何使用C#.net为Windows应用程序和使用C#ASP.NET的Web应用程序生成Excel文件 - how to generate excel file for a windows application using C# .net and a web application using C# ASP.NET 使用asp.net C#Javascript使用Finerprint登录到Asp.net Web应用程序 - Login into Asp.net Web application with Finerprint using asp.net C# Javascript 如何使用 C# 在 ASP.NET 中保护用户登录? - How to secure user login in ASP.NET using C#? 如何在使用C#作为服务器代码的ASP.NET Web应用程序中使用WF(Windows Workflow Foundation) - How to use WF (Windows Workflow Foundation) in a ASP.NET web application using C# as Server Code 如何在ASP.NET C#中实现用户会话 - how to implement user sessions in asp.net c# C#中的ASP.NET会话 - ASP.NET Sessions in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM