在本地存储“记住我”信息c#(new newbeginner)

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

我正在建立一个用户登录我的应用程序的系统,该系统正在运行。 我希望有一个“记住我” – 设置信息存储在本地的位置。 做这个的最好方式是什么? 我只保存用户名和密码哈希。

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

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

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

 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"]; 

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

这是你要做的。

别忘了加密它。

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

 FormsAuthentication.SetAuthCookie(model.UserName, true); 

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

我从你的问题中理解的是,php文件是服务器,对于客户端你使用的是Windows窗体。 您正在进行某种HTML报废,并在您的赢取forms中显示结果HTML。 如果这就是你正在做的事情

 //1. Create a dictionary to store cookie collection public static Dictionary 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是身份validationcookie的名称。 唯一的缺点是当应用程序存在时,字典中存储的所有cookie都将消失。 解决方案可能是序列化cookie并将其存储在数据库中,如果记住我的话。