Set cookie from IE hosted windows user control

Hi everybody.

What do I have:

I have a (c# VS 7) Web app that uses an Internet Explorer hosted Windows User Control.

Problem:

I need to set a cookie on clients' machine from the IE hosted control. Does somebody know how to do that

I would appreciate any suggestions.

Thanks in advance!

Bozesan Mihai




Answer this question

Set cookie from IE hosted windows user control

  • DotNetRulezZ

    Use InternetSetCookie method to set cookies.

    You can use InternetGetCookie to retrieve cookies for the specified URL and all its parent URLs.

    A lillte resource page: Managing Cookies.


  • Spoodles

    Can I set the time to live for a cookie with this functions



  • jbella

    Here is the error message:


    See the end of this message for details on invoking
    just-in-time (JIT) debugging instead of this dialog box.

    ************** Exception Text **************
    System.Security.SecurityException: System.Security.Permissions.SecurityPermission
    at AuthX.XLogIn.btnOk_Click(Object sender, EventArgs e)
    at System.Windows.Forms.Control.OnClick(EventArgs e)
    at System.Windows.Forms.Button.OnClick(EventArgs e)
    at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
    at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
    at System.Windows.Forms.Control.WndProc(Message& m)
    at System.Windows.Forms.ButtonBase.WndProc(Message& m)
    at System.Windows.Forms.Button.WndProc(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    The type of the first permission that failed was:
    System.Security.Permissions.SecurityPermission
    The Zone of the assembly that failed was:
    MyComputer


    ************** Loaded Assemblies **************
    mscorlib
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
    ----------------------------------------
    System
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
    ----------------------------------------
    AuthX
    Assembly Version: 1.0.2294.18246
    Win32 Version: n/a
    CodeBase: http://localhost/WebServer/AuthX.dll
    ----------------------------------------
    System.Windows.Forms
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
    ----------------------------------------
    System.Drawing
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
    ----------------------------------------
    Accessibility
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Accessibility/2.0.0.0__b03f5f7f11d50a3a/Accessibility.dll
    ----------------------------------------
    Microsoft.mshtml
    Assembly Version: 7.0.3300.0
    Win32 Version: 7.0.3300.0
    CodeBase: file:///C:/WINDOWS/assembly/GAC/Microsoft.mshtml/7.0.3300.0__b03f5f7f11d50a3a/Microsoft.mshtml.dll
    ----------------------------------------
    System.Web.Services
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Web.Services/2.0.0.0__b03f5f7f11d50a3a/System.Web.Services.dll
    ----------------------------------------

    ************** JIT Debugging **************
    To enable just-in-time (JIT) debugging, the .config file for this
    application or computer (machine.config) must have the
    jitDebugging value set in the system.windows.forms section.
    The application must also be compiled with debugging
    enabled.

    For example:

    <configuration>
    <system.windows.forms jitDebugging="true" />
    </configuration>

    When JIT debugging is enabled, any unhandled exception
    will be sent to the JIT debugger registered on the computer
    rather than be handled by this dialog box.





  • OldNick

    Yes you can as you can see on the documentation.

    Here is a little example:


    InternetSetCookie("http://msdn.microsoft.com", NULL,
    "TestData = Test; expires = Sat,06-Jan-2007 00:00:00 GMT");




  • Scott J Baldwin

    Can you be more specific on that please Maybe you could give me a small sample code, including the namespace that I have to use!
    Thanks!


  • Rxra

    You can use p/invoke to use the methods from wininet.dll, here are the p/invoke declarations:


    [DllImport("wininet.dll", CharSet=CharSet.Auto, SetLastError=true)]
    public static extern bool InternetGetCookie(
    string lpszUrlName,
    string lpszCookieName,
    StringBuilder lpszCookieData,
    [MarshalAs(UnmanagedType.U4)]
    ref int lpdwSize
    );


    [DllImport("wininet.dll", CharSet=CharSet.Auto, SetLastError=true)]
    public static extern bool InternetSetCookie(
    string lpszUrlName,
    string lpszCookieName,
    string lpszCookieData
    );


    [DllImport("wininet.dll", CharSet=CharSet.Auto, SetLastError=true)]
    public static extern bool InternetSetOption(
    int hInternet,
    int dwOption,
    string lpBuffer,
    int dwBufferLength
    );



    - Call InternetGetCookies to retrieve all cookies in a process

    - Call InternetSetCookies to reset the session ( which will reset
    cookies in a process)

    usage:

    // 0 - means all internet handles in same process
    InternetSetOption(0,42,null,0);




    - IF you want to set cookies for the new instance Call
    InternetSetCookie function which will set cookies.

    One thing to note here is InternetGetCookie function will give you a
    list of all cookies seperated by ";" . you need to do a bit string
    parsing to get individual cookies and set.

    Don't forget to prefix your url's with "http://", otherwise the method will break.


    InternetSetCookie("http://msdn.microsoft.com","mycookie","val=1&flag=0");


    will set the cookie mycookie for entire msdn.microsoft.com domain.


  • bobygeorge

    Thanks! You're really helping me. The local security settings won't allow me to place that cookie from the windows user control with InternetSetCookie. Do you know if I can handle that from my managed code


  • Set cookie from IE hosted windows user control