Reputation: 514
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.**
Upvotes: 0
Views: 1003
Reputation: 7856
An alternative way to encapsulate the logic geting current user - using custom ModelBinder For example:
public class UserBinders : System.Web.Mvc.IModelBinder
{
private const string SessionKey = "CurrentUser";
public object BindModel(ControllerContext controllercontext, System.Web.Mvc.ModelBindingContext bindingContext)
{
HttpContextWrapper httpcontext = new HttpContextWrapper(System.Web.HttpContext.Current);
string userLogin = httpcontext.User.Identity.Name
var currentUser = (User)controllercontext.HttpContext.Session[SessionKey];
if (currentUser == null)
{
currentUser = context.GetUserByLogin(userLogin);
controllercontext.HttpContext.Session[SessionKey] = currentUser;
}
return currentUser;
}
}
Just declare the variable type User and get the current user.
public ActionResult UserDetaiks(User CurrentUser)
{
string username = CurrentUser.UserFName;
return View()
}
Upvotes: 0
Reputation: 2378
Initialize in constructor :
public class HomeController : BaseController
{
private User CurrentUser;
public HomeController()
{
CurrentUser = Session["CurrentUserSession"] as User;
}
public ActionResult Index(string name)
{
return View();
}
public ActionResult UserDetaiks()
{
string username = CurrentUser.UserFName;
return View()
}
}
Upvotes: 0
Reputation: 23945
In my opinion this question is better suited at CodeReview, but here we go:
Your code requires the user to visit Index
first to assign a value to your currentuser variable. If a user visits details first, you'll get a NullReferencEception
.
In the end you'd better be off, with assing the current user in the ActionMethod, if you do not need it in every method. If you need in every one of your actions, you can initialize it in your constructor
public class HomeController : BaseController
{
public ActionResult Index(string name)
{
User CurrentUser = (User)Session["CurrentUserSession"];
return View();
}
public ActionResult UserDetails()
{
User CurrentUser = (User)Session["CurrentUserSession"];
string username = CurrentUser.UserFName;
return View()
}
}
OR:
public class HomeController : BaseController
{
private static User CurrentUser;
public HomeController()
{
CurrentUser = (User)Session["CurrentUserSession"];
}
/* ... */
}
In any case you should check if the Session variable is not null. I suppose if there is no user logged in, this could get nasty.
Upvotes: 1
Reputation: 14827
Since there is no guarantee, that the Index Method will be called before the UserDetails method, I wouldn't use a static variable to store the User. Instead each controller method should get the User from the session as required.
Upvotes: 1