Austyn Mahoney
Austyn Mahoney

Reputation: 11408

Sending cookies from C# Application to an Internet Explorer pop-up window

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?

Upvotes: 1

Views: 10661

Answers (3)

Spider C.
Spider C.

Reputation: 26

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.

Upvotes: 1

Spider C.
Spider C.

Reputation: 11

I've only been able to do this for an embedded WebBrowser control (see code below) but NOT for new IE window. Still haven't found an answer.

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

string url = "http://www.google.com";

// write the session cookie
InternetSetCookie(url, null, "abc=1");

webBrowser1.Navigate(url);

Upvotes: 1

Riza
Riza

Reputation: 1

You can use the usual session mode.

e.g:

Session.Add("<sessionName>", <variable which contains the session data>>

If you want to hold the username which is in a variable called "User", you can use the below code:

Session.Add("UserName", User);

In order to validate this session in your webpage, use the below code under the page_lad method.

if(Session["UserName"] != null)
{
   ********
}
else
{
   ******* (you can redirect to your home page...)
}

Upvotes: -1

Related Questions