Reputation: 717
In this picture of my model, view, controller and LocalDb, at the moment, I can get the UserName of the person that is logged in to appear on the manage page, but that data is coming from IPrincipal and not from the controller (I think), and I'm not sure how that works. I would like to pass the LastName of Roland (for example) to my Manage view, if Roland is currently logged in. How would you go about doing that?
Upvotes: 1
Views: 820
Reputation: 191
It's very simple. To save an object in session:
CurrentUserData cs = new CurrentUserData();
Session["UserData"] = cs;
To retreive the object from session:
CurrentUserData c = (CurrentUserData )Session["UserData"];
String userName = c.UserName;
String userSurname = c.UserSurname ;
(note that you should cast to CurrentUserData, Session["UserData"] retreive an object).
To delete the object from session
Session.Remove("UserData");
Advice: manage session variable names inside an Enum, so you don't hardcode it. Session[EnumSessionConsts.CurrentUserConst] (where EnumSessionConsts.CurrentUserConst == "userData").
PD. Set you're logged user data after the ok login (note that you always can save objects in session).
Upvotes: 2
Reputation: 191
I'm currently developing an enterprise system and had the same problem. Considering that your're using an auth framework, you're only able to access your current logged in user ID and status, not other info (correct me if i'm wrong). My workaround was to manage other related info (in my case user name, id, profile) in session as "Session const". BE CAREFULL TO DELETE THIS INFO ON USER LOGOUT. Code: Session[EnumSessionConsts.CurrentUserConst] = usr; where usr is a datatype with the info needded and EnumSessionConsts contains the name of the variable as const (do no hardcode). Then, in the controller (or view) you'll be able to access it. AGAIN! DO NOT FORGET TO DELETE THIS INFO. Hope this helps! and sorry for my english.
Upvotes: 2