Wednesday, February 9, 2011

Cookies and Objects

I'm having trouble figuring out how to access a cookie from a compiled object. I'm trying to make a compiled (DLL) object that will check the users cookie and then compare that to a database to confirm they have the correct access.

I can pass in the cookie info fine and the component will work, but I'm trying to have the component check the users cookie as well. I'm not even sure what object to use. I've been searching all weekend and I've seen references to httprequest, httpcookie, cookie, and cookiecollection.

I can look up cookie values on the page itself using Request.Cookies("inet")("user_id") but this doesn't work in the component.

  • Objects (App_Code/ compiled dlls) can only access Request via the static HttpContext.Current object

    HttpCookie cookie = HttpContext.Current.Request.Cookies["CookieName"];
    

    (If it's not called from a web app, HttpContext.Current is null, so you may want to check for that when running in unit testing) (If this isn't App_Code, you'll need to reference System.Web)

    From martin
  • If the component is a separate DLL from your web app you'd need to pass in a reference to the Request object.

    That said why not just read/check the cookie value in your ASP.NET code before calling into your DLL. It's not such a good idea to have your business logic coupled to your web tier like this.

    From Kev

0 comments:

Post a Comment