保持登录的用户信息 [英] Persisting logged in user information

查看:41
本文介绍了保持登录的用户信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个简单的概念,但我需要一些帮助.我正在用 c# 创建一个 winforms 应用程序,我正在尝试组织我的代码.第一个屏幕是登录,一旦用户通过身份验证,我将返回用户详细信息.我应该如何/在哪里存储这些详细信息,以便每次我想使用它们时都不必再次检索它们?

I know this is a simple concept, but I need some help. I'm creating a winforms app, in c#, and I'm trying to organize my code. The first screen is a log in, and once the user is authenticated I return the users details. How/where should I store these details so that I don't have to retrieve them again each time I want to use them?

谢谢!

推荐答案

与其他人所说的全局静态类相同,但有一些示例代码:

Along the lines of what the others have said about the global static class, but with some sample code :

public class UserInfo
{
    private int userID;

    public int UserID
    {
        get { return userID; }
    }

    private string userName;

    public string UserName
    {
        get { return userName; }
    }

    public UserInfo(int userID, string userName)
    {
        this.userID = userID;
        this.userName = userName;
    }
}

public static class GlobalInfo
{
    private static UserInfo currentUser;

    public static UserInfo CurrentUser
    {
        get { return currentUser; }
        set { currentUser = value; }
    }
}

所以,在用户登录后,保存登录信息:

So, after the user has logged in, save the logged in info :

GlobalInfo.CurrentUser = new UserInfo(123, "Bob Jones");

当您需要获取信息时:

UserInfo userInfo = GlobalInfo.CurrentUser;

这篇关于保持登录的用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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