ASP.NET MVC storage for users in a session

I am developing an ASP.Net MVC 3 web application with Entity Framework 4. When a user enters my application, I would like to save his user object (firstName, lastName, etc.) in a session, which can then be accessed throughout expression.

I understand that this may not be a good idea, because when the ObjectContext is closed / deleted, the user object is separated, and the user details may be lost.

I thought that another method could be, when the user logs in, assign the user ID (primary key) to the session variable, i.e.:

HttpContext.Current.Session["currentUserID"] = user.userID;

Then create a class in the UserService class as follows:

public static User CurrentUser
    {

        get
        {
            return Data.DBEntities.Users.Where(u => u.userID == HttpContext.Current.Session["currentUserID"]).FirstOrDefault();
        }

    }

, currentUserID. , ,

Cannot convert lambda expression to type 'string' because it is not a delegate type
Delegate 'System.Func<Asset.Model.User,int,bool>' does not take 1 arguments

, ?

.

+5
2

-, Session. Google " ASP.NET" , .

, . . , , Session . :

public static User CurrentUser
{
    get
    {
        object userID = HttpContext.Current.Session["currentUserID"];
        if (userID == null)
        {
            throw new InvalidOperationException("Oops!");
        }
        return Data.DBEntities.Users.Where(u => u.userID == (int)userId ).FirstOrDefault();
    }
}

... , , , .

, .

+9

. , , - .

currentUserId .

0

All Articles