How can I read the properties of an object that I assign to the Session in ASP.NET MVC?

Posted by quakkels on Stack Overflow See other posts from Stack Overflow or by quakkels
Published on 2010-06-02T20:34:38Z Indexed on 2010/06/02 20:44 UTC
Read the original article Hit count: 346

Filed under:
|
|
|

Hey all,

I'm trying my hand at creating a session which stores member information which the application can use to reveal certain navigation and allow access to certain pages and member role specific functionality.

I've been able to assign my MemberLoggedIn object to the session in this way:

    //code excerpt start...

    MemberLoggedIn loggedIn = new MemberLoggedIn();

    if (computedHash == member.Hash)
    {
        loggedIn.ID = member.ID;
        loggedIn.Username = member.Username;
        loggedIn.Email = member.Email;
        loggedIn.Superuser = member.Superuser;
        loggedIn.Active = member.Active;

        Session["loggedIn"] = loggedIn;
    }
    else if (ModelState.IsValid) {
        ModelState.AddModelError("Password", "Incorrect Username or Password."); 
    }

    return View();

That works great. I then can send the properties of Session["loggedIn"] to the View in this way:

    [ChildActionOnly]
    public ActionResult Login()
    {
        if (Session["loggedIn"] != null)
            ViewData.Model = Session["loggedIn"];
        else
            ViewData.Model = null;
        return PartialView();
    }

In the Partial View I can reference the session data by using Model.Username or Model.Superuser.

However, it doesn't seem to work that way in the controller or in a custom Action Filter. Is there a way to get the equivalent of Session["loggedIn"].Username?

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET