views:

1476

answers:

4

I have a C# form that we browse our web application with. If we are inside the form and logged in and we click a link that opens a new window (an Internet Explorer window), it asks for my credentials again.

We use cookies to store the session for authentication. Is there a way to make the account stay logged in whether it is in the form or in a separate IE window?

I assume this is a matter of passing cookies from the form to the Internet Explorer window for authentication.

I can provide more information if needed.

A: 

I am also experiencing a similar challenge. We are attempting to wrap an external website inside a C# application, but when the external website opens an internet explorer window, the authentication must be re-done.

Has anyone found a solution to this problem?

Ben D.
If you have a follow up question it's better to post it as a new questionthan as an answer to an old question.More people will read it since old questions are not frequented very much.In the top right is an "Ask Question" button to do so.
sth
+1  A: 

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);

Spider C.
It seems there isn't a way to persist sessions from a C# application to an IE window.
Austyn Mahoney
+1  A: 

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

    (a) constructor to pass the target url, set it to the Address bar, set it as the home page and also pass the security token

    (b) properties to enable/disable the Address bar and Search bars

    (c) added Cut, Copy, Paste, SaveAs, and Print buttons

    (d) 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.

Spider C.
Definitely not an easy solution, but thanks for the answer! We decided a long time ago to just let the user login again, but it's good to know what needs to be if we ever decide otherwise.
Austyn Mahoney
A: 

you can use the usual session mode..

e.g:

Session.Add("", >

e.g: 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 calidate 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...) }

Riza