1

I want to define a cookie value as class variable in constructor method to make all methods are available to use the Cookie.

but I got an error message like,

Object reference not set to an instance of an object.

 public class OrdersController : Controller
{   
    string userData;

    public orderConroller(){
       string cookieName = FormsAuthentication.FormsCookieName;
           HttpCookie authCookie = Request.Cookies[cookieName];
       FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
           userData = authTicket.UserData;
    }

    public void a(){
      //I need Cookie
    }
    public void b(){
      //I need Cookie
    }
    public void c(){
      //I need Cookie
    }
    public void d(){
      //I need Cookie
    }
}

How can I solve this problem? @.@

Thank you!

3 Answers 3

6

You cannot use the HttpContext (in your case you are attempting to access the Request object) inside a controller constructor because it is not yet initialized. The earliest method in which you could access it is the Initialize method that you could override.

So:

public class OrdersController : Controller
{   
    private string userData;

    protected override void Initialize(RequestContext requestContext) 
    {
        base.Initialize(requestContext);
        var request = requestContext.HttpContext.Request;
        string cookieName = FormsAuthentication.FormsCookieName;
        HttpCookie authCookie = request.Cookies[cookieName];
        FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
        userData = authTicket.UserData;
    }

    public void a() {
      //I need Cookie
    }
    public void b() {
      //I need Cookie
    }
    public void c() {
      //I need Cookie
    }
    public void d() {
      //I need Cookie
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I see your need for cookies is about authentication. For authentication, the best way would be to use a custom authorization filter. The filter could initialize a User object with all the piece of information needed. Way more elegant than having this code inside a constructor.

Comments

0

You can make the userdata available on a lazy fashion:

public class OrdersController : Controller
{   
    private string userData;
    private string UserData {  
        get { 
            if(userData == null) {
                var request = requestContext.HttpContext.Request;
                string cookieName = FormsAuthentication.FormsCookieName;
                HttpCookie authCookie = request.Cookies[cookieName];
                FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
                userData = authTicket.UserData;
             }
             return userData;
         }
     }
...
}

Then in your actions you just call the UserData property. This way you won't be executing that code in any request, but just in the ones that need the user data (considering you are decrypting things, you may want to avoid doing that in every request if you don't need to).

Hope it helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.