BAPSDude
BAPSDude

Reputation: 341

Keep Me Signed In code in Silverlight

Can anyone provide the code for "Keep me Signed In"(on Login Page) functionality in silverlight 4.0?

I have searched a lot but not getting any appropriate code.

Upvotes: 0

Views: 129

Answers (1)

Farhad Jabiyev
Farhad Jabiyev

Reputation: 26655

You can use Cookies or IsolatedStorageSettings. I have once used Cookies in my Silverlight Application.

private void SetCookie(string key, string value)
{
    DateTime expiration = DateTime.UtcNow + TimeSpan.FromDays(2000);
    string cookie = String.Format("{0}={1};expires={2}", key, value, expiration.ToString("R"));
    HtmlPage.Document.SetProperty("cookie", cookie);
}

private string GetCookie(string key)
{
    string[] cookies = HtmlPage.Document.Cookies.Split(';');

    foreach (string cookie in cookies)
    {
        string[] keyValue = cookie.Split('=');

        if (keyValue.Length == 2 && keyValue[0].Trim() == key)
        {
            return HttpUtility.UrlDecode(keyValue[1]);
        }
    }

    return null;
}

So you can set cookies whenever you want and get them like that:

 SetCookie("WebUsername", UsernameTextBox.Text);
 SetCookie("WebPassword", PasswordtextBox.Text);

 UsernameTextBox.Text = GetCookie("WebUsername");
 PasswordBox.Password = GetCookie("WebPassword");

Upvotes: 1

Related Questions