Hi we developing web application using mvc4 and jquery mobile. In our each controller we created user session like below.
public class HomeController : BaseController { private static User CurrentUser;
public ActionResult Index(string name) { CurrentUser = (User)Session["CurrentUserSession"]; return View(); } public ActionResult UserDetaiks() { string username = CurrentUser.UserFName; return View() } }
Above we created object for User model and assigned session value in index method. But the value in CurrentUser is lost once i entered UserDetails. So i Used static while creating object. My question is it correct? or anyother way is there. Please guide me. Thanks guys One more Doubt,
I used below code in form authenticate.
HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; if (authCookie != null) { FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value); var s = new System.Web.Script.Serialization.JavaScriptSerializer(); User obj = s.Deserialize<User>(authTicket.UserData); UserInformation CurrentUser = new UserInformation(obj.UserID); CurrentUser.UserId = obj.UserID; CurrentUser.FirstName = obj.UserFName; CurrentUser.LastName = obj.UserLName; CurrentUser.roles = obj.SecurityGroupName; CurrentUser.DefaultWarehouseId = obj.DefaultWhseIdentity; eg:14 HttpContext.Current.User = CurrentUser; } **Here User can Change CurrentUser.DefaultWarehouseId later like CurrentUser.DefaultWarehouseId = 16. but when i leave the method. It again getting value 14. Now i can code so CurrentUser.DefaultWarehouseId will be 16 through out app one i changed, Please guide me.**
staticwon't work.staticis not per-request. It's actually shared through the life-time of your application pool. In short, don't do it.