Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8114679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:13:03+00:00 2026-06-06T03:13:03+00:00

All my controller has this code on the top of class. public class TestController

  • 0

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]

enter image description here

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T03:13:06+00:00Added an answer on June 6, 2026 at 3:13 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code in my controller: @cats = DirCat.all And this in a
How could I write code to get all the action names from a controller
I've got controller code like this all over my ASP.NET MVC 3 site: [HttpPost]
I've model called BlogPost and controller called BlogPostsController that has all basic CRUD methods
I have this simple code in my user_controller.rb file #listing all users def index
Having this code : Models paciente.rb class Paciente include SimplyStored::Couch has_many :consultas end consulta.rb
I have an jQuery autocomplete field with non latin characters. My Controller has this
I am trying to generate a controller with all the RESTful actions stubbed. I
as we all know that html_safe work in controller and view i am sending
I am writing a catch all method for my controller for ajax. It is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.