Expert wanna be
Expert wanna be

Reputation: 10624

Asp.net MVC3, using constructor

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!

Upvotes: 1

Views: 245

Answers (3)

Ivo
Ivo

Reputation: 8372

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.

Upvotes: 0

Christophe Argento
Christophe Argento

Reputation: 193

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.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039508

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
    }
}

Upvotes: 6

Related Questions