OptimusCrime
OptimusCrime

Reputation: 14863

Storing "remember me" information locally c# (total newbeginner)

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

Upvotes: 2

Views: 3787

Answers (4)

Anand
Anand

Reputation: 14935

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.

Upvotes: 0

Shai
Shai

Reputation: 25595

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

Upvotes: 4

shift66
shift66

Reputation: 11958

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.

Upvotes: 0

John Woo
John Woo

Reputation: 263803

you can create Application Settings in C#

here's what you will do.

don't forget to encrypt it.

Upvotes: 0

Related Questions