Saber Amani
Saber Amani

Reputation: 6489

Keep Temp Data For Each User

I want to know what is the best way to keep data such as simple ID for each user if I won't use Session in ASP.Net and ASP.Net MVC applications?

Upvotes: 0

Views: 283

Answers (2)

Lars Anundskås
Lars Anundskås

Reputation: 1355

You could store it in the user profile, this can be extended by inheriting from ProfileBase and you can implement your own properties.

For example:

public class ProfileManager : ProfileBase
{
public ProfileManager() : base()
{

}

[SettingsAllowAnonymous(false)]
[ProfileProvider("AspNetSqlProfileProvider")]
public string DisplayName {
    get
    {
        return base["DisplayName"].ToString();
    }
    set
    {
        base["DisplayName"] = value;
        this.Save();
    }
}
}

In your config, you need to tell your application what type your profile is of:

<profile inherits="ProfileNamespace.ProfileManager" defaultProvider="AspNetSqlProfileProvider" enabled="true">
<providers>
<clear />
<add name="AspNetSqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</providers>
</profile>

Upvotes: 4

Darin Dimitrov
Darin Dimitrov

Reputation: 1039190

You could use cookies or if it is just the ID of the currently authenticated user, the Forms Authentication cookie seems exactly designed for this purpose. The forms authentication cookie could be extended and add some other custom user info that you might need on each request (such as the First and Last name of the user). This would avoid you to hit the database on each request. You could use the UserData portion of the Forms Authentication cookie.

Upvotes: 4

Related Questions