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!
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:
Then, anytime you need to use it, you just call
MemberModelProvider.Current.