kamiar3001
kamiar3001

Reputation: 2676

Membership provider class

I want to know how I can implement membership provider class to have ability to remember users who signed in.

I have Membership provider class and I need functionality of "Remember Me" checkbox but I don't know how I can implement some methods

Upvotes: 0

Views: 612

Answers (3)

womp
womp

Reputation: 116977

"Remember Me" doesn't have anything to do with a Membership Provider really. Basically it is just a function of Forms Authentication, where you set a persistent cookie so that when people show up at the website, it can log them in automatically.

You can do this automatically using the RedirectFromLoginPage() method.

FormsAuthentication.RedirectFromLoginPage(username, true);

The second parameter, "true", means "set a persistent cookie". The user will be logged in until the cookie expires or they clear their cookies.

If you need more control over it, you can manually set a cookie by manipulating the cookies collection directly.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

In order to implement this functionality you must create a persistent cookie with some expiration date on the users computer. So if the user checks the Remember me checkbox you issue the following cookie:

var cookie = new HttpCookie("_some_cookie_name_", "username") 
{
    Expires = DateTime.Now.AddDays(15) // Remember user for 15 days
};
Response.Cookies.Add(cookie);

And then upon showing the login screen you could check if the cookie is present and prefill the username:

var cookie = Request.Cookies["_some_cookie_name_"];
if (cookie != null)
{
    usernameTextBox.Text = cookie.Value;
}

Upvotes: 2

Alex G.
Alex G.

Reputation: 1

I would use a Hashtable if it's in C#, keyed by the user id. Something like this (where lsdfjk is just whatever string the user ID corresponds to, and assuming that there is a class UserInfo defined, with a constructor taking string userID as an argument):

        string userID = "lsdfjk";
        UserInfo userInfo = null;
        Hashtable htMembers = new Hashtable();
        if (htMembers.ContainsKey(userID))
        {
            userInfo = (UserInfo)htMembers[userID];
        }
        else
        {
            //It's a new member
            userInfo = new UserInfo(userID);
        }

Upvotes: 0

Related Questions