views:

132

answers:

3

Hello, all.

I 'm writting an application in ASP.NET MVC. Basically I have some pages which require user authentication. Upon user logon, I keep the user row in a session. So in my controller I can access to the user.ID without making extra queries.

When project is in debug mode I can only change things in the views. Not in the controller.

If I 'm not debugging, I can build the solution, and see the changes I made without running the project (with F5). BUT, it looses all session variables I have.

So basically for every no-matter how small change in the controller, I have to logoff, logon to see my changes.

Are those normal behaviours?

+1  A: 

Yes, this is a normal behaviour of ASP.NET as a whole not just MVC.

If you need to recompile (e.g a change in controller or business object) you will be in a new session when you run in debug. Like you say, only changes in views or pages (which do not require a recompile) will allow you to see changes in same session.

Kindness,

Dan

Daniel Elliott
So how do you debug large projects? Do you always logoff, loggon for viewing changes?Can we keep user details somewhere else? For example Request.IsAuthenticated is true besides the fact that session is lost. Can I save some credentials there?
gong
@gong take a look at my response below for handling the information.
Agent_9191
+1  A: 

Like Dan stated, this is normal behavior. To make it easier (and slightly more robust) is to change your code slightly. This is of course assuming that you are storing more than just the User ID in session since you can access the User ID via Controller.User.Identity.Name when they are authenticated. So you perform the lookup of the additional data in the session object and if it doesn't return null then use it. If it does return null, then look up the additional information again based on the User ID and store it in the session again. This is the approach I take for storing information from Active Directory and it works great.

Agent_9191
Yes I will do this. But isn't it db intensive to querying the database on every page to get user details?
gong
That's why you check Session first. You're only querying the DB if the Session is null.
Agent_9191
A: 

Recompiling will clear all the current session data. However it will not clear your authentication ticket, that's stored as a cookie, so there's a few things you can do to avoid this.

  1. If you only need to access the user id then use User.Indentity.Name

  2. If you only need basic user data for display purposes, such as the user's name, then you can store that in a session cookie. Warning: only do this for displaying data unless you encrypt the cookie data, plain text cookie data shouldn't be trusted.

  3. If your user data is more complex than that then access the data through a method that uses caching as suggested by Agent_9191

Add something like this to a base controller or extension method

protected UserData GetUserData() {
  UserData user = HttpContext.Session["User"] as UserData;
  if (user == null) {
    user = UserDataRepository.GetUser(User.Identity.Name);
    HttpContext.Session.Add("User", user);
  }
  return user;
}
David G