Q:

ASP.NET MVC3, How to set global variable which can be used in Controller and Model too?

All my controller has this code on the top of class.
public class TestController : Controller
{
//
// GET: /Reports/

string userName;
string uid;
string accountType;
int companyCode;

protected override void Initialize(RequestContext requestContext)
{
    base.Initialize(requestContext);

    string cookieName = FormsAuthentication.FormsCookieName;
    HttpCookie authCookie = Request.Cookies[cookieName];

    if (authCookie != null)
    {
    FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
    string userData = authTicket.UserData;

    JavaScriptSerializer serializer = new JavaScriptSerializer();
    var userInfo = serializer.Deserialize<MemberModel>(userData);

    userName = userInfo.firstName + " " + userInfo.lastName;
    uid = userInfo.uid;
    accountType = userInfo.accountType;
    companyCode = userInfo.companyCode;
    }
}

Because, every controller need to have user information that's in cookie.
I think this is not right way writing that code in every controller.
Also, Even I can not use that method in my MODEL.
base.Initialize(requestContext); <= 'object' does not contain a definition for 'Initialize'

So, I'd like to know how to set that method as GLOBAL variable to use in any controller and model.
Anybody knows, please advice me~ 
[EDIT]

Thank you!
[EDIT]
I define as above code in HomeController and try to call the userName in testController.
but it is empty. (Error is gone now)
In TestController,
[HttpGet]
public void test()
{
    Response.Write(MemberModelProvider.UserName);
    Response.Write(System.Web.HttpContext.Current.Items["UserName"]);
}

it print nothing...
What am I doing wrong? 
I very appreciate for your help!

A:

Create a common base class for controllers and make all actual controllers inherit that
public abstract class BaseController : Controller
{
    protected readonly MemberModel Member;

    protected BaseController()
    {
        Member = getUserInfoFromCookie();
    }
}
public class TestController : BaseController
{
}

A:

You can put that information to live in the HttpContext.Items. This collection lives while the request, so you won't have any troubles with different requests accessing the same code:
public abstract class AuthenticatedController : Controller
{

  protected override void Initialize(RequestContext requestContext)
  {
    base.Initialize(requestContext);

    string cookieName = FormsAuthentication.FormsCookieName;
    HttpCookie authCookie = Request.Cookies[cookieName];

    if (authCookie != null)
    {
    FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
    string userData = authTicket.UserData;

    JavaScriptSerializer serializer = new JavaScriptSerializer();
    var userInfo = serializer.Deserialize<MemberModel>(userData);
    MemberModelProvider.Current = userInfo;
    }
  }
}

public class HomeController :  AuthenticatedController {

    //Your actions here...
}

public class TestController :  AuthenticatedController {

    //Your actions here...
}

public class MemberModelProvider {
    public static MemberModel Current {
        get { return (MemberModel)HttpContext.Current.Items["CurrentMemberModel"];}
        set { HttpContext.Current.Items["CurrentMemberModel"] = value; }
    }
}

Then, anytime you need to use it, you just call MemberModelProvider.Current.

