Storing “remember me” information locally c# (total newbeginner)

僤鯓⒐⒋嵵緔 提交于 2019-12-06 10:28:16

问题


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


回答1:


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



回答2:


you can create Application Settings in C#

here's what you will do.

don't forget to encrypt it.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/8785502/storing-remember-me-information-locally-c-sharp-total-newbeginner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!