且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将Cookie从C#应用程序发送到Internet Explorer弹出窗口

更新时间:2023-10-19 23:22:04

我相信任何新的窗口实例将开始一个新的会话,因此消除您可能已创建的任何会话cookie,然后通过 Process.Start(iexplorer .exe,www.msn.ca) webBrowser1.Navigate(www.msn.ca,true) p>

然而,我终于找到了解决这个问题的工作。它包含以下内容:


  1. 创建自己的Web浏览器,看起来几乎就像Internet Explorer。有关基本浏览器的代码,请参阅下面的链接,并根据需要进行自定义。



    http://msdn.microsoft.com/en-us/library/ms379558(VS.80).aspx



    我修复了错误用ToolStrip,ToolStripContainer替换过时的RaftingStrip。 (



    对于我自己,我添加了


    1. 构造函数传递目标网址,将其设置为地址栏,将其设置为主页并传递安全令牌


  2. 添加剪切,复制,粘贴功能,SaveAs和打印按钮


  3. 将进度图标从C#更改为沙漏



  • 在网络浏览器表单的声明部分,添加

      DllImport wininet.dll,CharSet = CharSet.Auto,SetLastError = true)] 
    public static extern bool InternetSetCookie(string lpszUrlName,
    string lbszCookieName,string lpszCookieData);在web浏览器表单加载和Create_a_new_tab(),在网页之前.Navigate()请将以下行保存到会话cookie。 (如果导航到一个新域名,也可能需要在button_go_click和button_search_click事件上。)个人来说,如果通过安全令牌,我将禁用地址和搜索栏。)
      string url = comboBox_url.Text.Trim(); //写会话cookie 
    string security_token =my_session_id = 12345//应该已经传递给构造函数
    InternetSetCookie(url,null,security_token);


  • 用户永远不会知道差异可以控制要打开/关闭或实施的功能。


    I have a C# application that has links to certain features on a web application that uses the same login credentials. If the user has logged in using the application and they click a link, a new browser window opens (usually IE but may be just the default browser) and asks the them to authenticate again.

    Is there a way to make the account stay logged in whether it is in the C# application or in a separate browser window? The web application uses cookies to store the users session variables for authentication, the C# application has the same session info but not in a cookie.

    Essentially I need to create a cookie from the C# application's session information and pass it to the new browser window.

    Any ideas?

    I believe any new window instance will start a new session and therefore wipe out any session cookies you may have created prior to opening a new browser through either Process.Start("iexplorer.exe", "www.msn.ca") or webBrowser1.Navigate("www.msn.ca", true).

    However I finally found a work around for this problem. It involves the following:

    1. Create your own web browser that looks almost exactly like Internet Explorer. See the folowing link for code for a basic browser and customize it as needed.

      http://msdn.microsoft.com/en-us/library/ms379558(VS.80).aspx

      I fixed the bug by replacing the obsolete RaftingStrip with the ToolStrip, ToolStripContainer. (Email me if you want this debugged code. I'd post it but I think it's too long for this forum.)

      For myself, I added

      1. constructor to pass the target url, set it to the Address bar, set it as the home page and also pass the security token

      2. properties to enable/disable the Address bar and Search bars

      3. added Cut, Copy, Paste, SaveAs, and Print buttons

      4. changed the progress icon from "C#" to an hourglass

    2. On the web browser form's declaration section, add

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

    3. On the web browser form load and Create_a_new_tab(), before the webpage.Navigate() insert the lines below that save the token to a session cookie. (This may also be required on the button_go_click and button_search_click events if navigating to a new domain. Personally, I would disable the Address and Search bars if passing a security token.)

      string url = comboBox_url.Text.Trim();  // write the session cookie
      string security_token = "my_session_id=12345" // should have been passed to constructor
      InternetSetCookie(url, null, security_token);
      

    The user will never know the difference and you have control over what features to turn on/off or implement.