存储“记住我”本地信息c#(全新初学者)

发布于 2024-12-25 12:59:35 字数 204 浏览 3 评论 0原文

几天前我开始用 C# 编程,所以我在这方面完全是新手。根据我使用其他语言的经验,我发现它有点“简单”。

我正在构建一个系统,用户可以登录我的应用程序,该应用程序正在运行。我想要一个“记住我”设置,信息存储在本地。最好的方法是什么?我只会保存用户名和密码哈希。

编辑:这是一个桌面应用程序。只需使用 HttpWebRequest 即可将登录信息发送到 php 脚本

I started programming in c# for a few days ago, so I am a total newbeginner at this. Based on my experience in other languages, I found it somewhat "simple".

I am building a system where users are logging in to my application, which is working. I want to have a "remember me"-setting where the information is stored locally. What is the best way to do this? I'll only save the username and the password-hash.

Edit: This is a desktop-application. The login-information is sent to a php-script simply using HttpWebRequest

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风尘浪孓 2025-01-01 12:59:35

您可以使用 ConfigurationManager 类来管理应用程序的设置。

您可以使用此功能将新的密钥添加到您的配置文件中:

public bool setSetting(string pstrKey, string pstrValue)
{
    Configuration objConfigFile =
        ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    bool blnKeyExists = false;

    foreach (string strKey in objConfigFile.AppSettings.Settings.AllKeys)
    {
        if (strKey == pstrKey)
        {
            blnKeyExists = true;
            objConfigFile.AppSettings.Settings[pstrKey].Value = pstrValue;
            break;
        }
    }
    if (!blnKeyExists)
    {
        objConfigFile.AppSettings.Settings.Add(pstrKey, pstrValue);
    }
    objConfigFile.Save(ConfigurationSaveMode.Modified);
    ConfigurationManager.RefreshSection("appSettings");
    return true;
}

然后保存您的用户名(例如)

setSetting("username", usernameTextBox.Text);

一旦您的应用程序启动,您可以从 ConfigurationManager 读取您之前保存的信息

usernameTextBox.Text = ConfigurationManager.AppSettings["username"];

You can use the ConfigurationManager Class to manage your application's settings.

you can use this function to add new Keys to your configuration file:

public bool setSetting(string pstrKey, string pstrValue)
{
    Configuration objConfigFile =
        ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    bool blnKeyExists = false;

    foreach (string strKey in objConfigFile.AppSettings.Settings.AllKeys)
    {
        if (strKey == pstrKey)
        {
            blnKeyExists = true;
            objConfigFile.AppSettings.Settings[pstrKey].Value = pstrValue;
            break;
        }
    }
    if (!blnKeyExists)
    {
        objConfigFile.AppSettings.Settings.Add(pstrKey, pstrValue);
    }
    objConfigFile.Save(ConfigurationSaveMode.Modified);
    ConfigurationManager.RefreshSection("appSettings");
    return true;
}

and then save up your username (for example)

setSetting("username", usernameTextBox.Text);

Once your application starts up, you can read the information you saved earlier from your ConfigurationManager

usernameTextBox.Text = ConfigurationManager.AppSettings["username"];
不气馁 2025-01-01 12:59:35

您可以在 C# 中创建应用程序设置

这就是您要做的做。

不要忘记加密它。

you can create Application Settings in C#

here's what you will do.

don't forget to encrypt it.

左耳近心 2025-01-01 12:59:35

如果您使用的是 ASP .NET,则可以在用户登录时通过第二个参数设置身份验证 cookie。

FormsAuthentication.SetAuthCookie(model.UserName, true);

第二个参数将 cookie 设置为您的请求,并设置“记住我”选项。

If you're using ASP .NET,you can set authentication cookie when you're logged in user by second parameter

FormsAuthentication.SetAuthCookie(model.UserName, true);

Second parameter sets cookie to your request and makes "Remeber Me" option.

余生再见 2025-01-01 12:59:35

我从你的问题中了解到的是,php 文件是服务器,对于客户端,你使用的是 Windows 窗体。您正在执行某种 HTML 抓取并在 win 表单中显示结果 HTML。如果这就是您正在做的事情,

//1. Create a dictionary to store cookie collection

    public static Dictionary<string, Cookie> CookieCollection { get; set; }

//2. Store cookie in that collection


foreach (Cookie clientcookie in response.Cookies)
     {
                    if (!CookieCollection.ContainsKey("AuthCookieName"))
                        CookieCollection .Add(userName, clientcookie);
                    else
                        CookieCollection ["userName"] = clientcookie;
    }

//3. If remember me is clicked then send the same while creating request

    request.CookieContainer.Add(request.RequestUri, 
new Cookie("AuthCookieName", CookieCollection ["userName"]));

那么 AuthCookieName 是身份验证 cookie 的名称。唯一的缺点是当应用程序存在时,存储在字典中的所有 cookie 都会消失。如果选中“记住我”,解决方案可以是序列化 cookie 并将其存储在数据库中。

What I understand from your question is, php file is server and for client you are using windows form. Your are doing some kind of HTML scrapping and displaying the result HTML in your win-form. If this is the what you are doing then

//1. Create a dictionary to store cookie collection

    public static Dictionary<string, Cookie> CookieCollection { get; set; }

//2. Store cookie in that collection


foreach (Cookie clientcookie in response.Cookies)
     {
                    if (!CookieCollection.ContainsKey("AuthCookieName"))
                        CookieCollection .Add(userName, clientcookie);
                    else
                        CookieCollection ["userName"] = clientcookie;
    }

//3. If remember me is clicked then send the same while creating request

    request.CookieContainer.Add(request.RequestUri, 
new Cookie("AuthCookieName", CookieCollection ["userName"]));

Where AuthCookieName is the name of authentication cookie. The only downside is when the application exists all the cookie stored in the dictionary would be gone. The solution could be serializing the cookie and storing it in database, if remember me is checked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文