How to start new IE session with Watin - c#

I need to know how to start a browser instance but with a new session. I tried to clear cookies and clear cache but this doesn't give me what I need.
In my case I add products to a basket, then close the browser, then use another url that adds different product, but always the old products are still in the basket.

for IE6-7 you should start a new browser session in a new process like this:
var ie = new IE("yoururl", true);
for IE8-9 you should set the no-merge option on the settings class before creating a new IE instance:
Settings.MakeNewIe8InstanceNoMerge = true;
var ie = new IE("yoururl");

Make sure that you're closing the browser properly (i.e. closing all its tabs and windows, including downloads and such) - look at the task manager to see if it's still running. Session might not be cookie-based (for example, it could be tracked in the same process that processes the request), and clearing browser cache isn't going to affect the open sessions in any way.
Assuming that you're developing an asp.net application or website you may try to right-click on the ASP.NET Development Server icon in the system tray and stop it, then re-run your project. That should kill all sessions.

We had the same problem here. IE re-uses the session over all its browser instances. If you want a new session just do:
Alt (menu) -> File -> New Session
Check this blog for more info.

Sessions are managed by cookies, you can clear all your cookies or just for the site concerned.
Try searching for IE Clearing cookies

IE 9 has one more way of not clearing all the cookies.
If you open the Dialog to Clear Cookies from the Options Menu in IE 9, you will notice that the first options says "Preserve Favorites Website Data." Leave this unchecked and you usual clear cookies, close browser should work.
Let me know if this helps, I think this should do the trick ;)

I will be trying this in WatiN soon, but just testing with my commandline switches, it looks like I can open multiple instances using
iexplore.exe -private -nomerge
and each instance will not interfere with the other ones.
This means it allows you to login with differnet logins on the same website, and they won't interfere with each other.
This needs further verificaiton, but so far it seems to work.
update: I found this old patch for WatiN that adds the privacy feature to the watin lib, it doesn't appear to currently be in the release- http://sourceforge.net/tracker/?func=detail&aid=3013950&group_id=167632&atid=843730

Related

Clear Cookies for my WebView2 Instance in C#

I have a small application that logins into a website and performs some actions. I need a method to clear the cookies and essentially "Force" logout of the website so I can perform the actions again. The easiest way is to just clear the cookies and then repeat the login process, but I haven't been able to figure out how. I have found this resource from Microsoft but I haven't been able to locate any sample code
https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2cookiemanager.deleteallcookies?view=webview2-dotnet-1.0.1462.37
I believe WebView2 and Edge are closely related and also want to make sure by doing this I do not end up clearing the cookies on their main browser if they use Edge
Any assistance is greatly appreciated
If you have a WebView2 class you can do the following to clear cookies and reload the page:
// Clear cookies for this WebView2 and all WebView2s
// sharing the same user data folder.
webview2.CoreWebView2.CookieManager.DeleteAllCookies();
// Reload the document after clearing cookies
webview2.CoreWebView2.Reload();
The above code must be run after the WebView2 has finished initializing its CoreWebView2.
When you create a CoreWebView2 it belongs to a user data folder and browser process. Changing cookies via the CookieManager applies to all CoreWebView2s sharing the same profile and that user data folder.
You cannot share state between the WebView2 and the browser so you will not be modifying cookies in the browser.
How about using Private mode?
MSDN interface ICoreWebView2ControllerOptions
You can clear cookies only dispose instance.

Chrome cookie not up-to-date

In my Winforms project, I can get a cookie of a site opened in IE by the following method :
InternetGetCookie("mysite.com", "mycookie", "something" , "something" )
As a new requirement coming, the site must be opened in Chrome. That means the method above doesn't work anymore.
After some research, I found out a solution to use Sqlite to read the cookies file stored in "Users\xx\AppData\Google\Chrome\User Data\Default\cookies", it works as expected. I can fetch the cookie by giving the name and URL.
BUT PROBLEM: The cookies file is not up-to-date and is updated 1-2 minutes laters. That means the cookies of the request shown in Chrome DevTool is not the same as in the cookies file.
Is there any way to fetch the cookie in Chrome from C# Winforms project similar to InternetGetCookie?
Hmm, there doesn't seem to be a chrome flag to flush this quicker so probably not going to be an easy option... You could maybe:
Grab it from memory (may be possible if you can search for the value somehow)
Write a Chrome extension which dumps it immediately
Use a headless browser instance to visit the site and send the cookie back instead

Webkit embedded browser disable cookies

I'm using this borwser in .net 3.5 winform application, on x86 platform.
http://sourceforge.net/projects/webkitdotnet/
The problem, when I logged in facebook, with this browser, and I've got the data from facebook, and I closed the form, and after I reloaded the form (every control recreated) the facebook logged again. So the session continued. I don't want this, I'would like to begin a new session. Can I disable the cookies of browser, or reset session, or something like this?
public WebBrowserTabPage currentPage;
WebBrowserTabPage page = new WebBrowserTabPage();
tabControl.TabPages.Add(page);
currentPage = page;
currentPage.browser.Navigate(Url);
currentPage.browser is a WebKitBrowser instance.
I've seen about my problem, and I found a variable (CookiesPolicy in WebkitBrowser class. I've set up this to disable cookies, and facebook didn't worked without cookies. I've tried to delete the WebkitBrowser's cookies from hard drive, but I didn't find them. After I've dicovered, there is a memory leak with Webkitbrowser. So I closed the from, but it didn't disposed, and stayed in memory. Finally I solved the problem, I've put the WebkitBrowser in another Project, and I'm running in different Application.
I'm using Inter Process Communication (IPM) for communicating between mother form application and the browser application
After the browsers
Application.Exit();
the session is disposed, and I can sign into facebook again.
Here is the sample code for IPM with named Pipe:
http://www.codeproject.com/Articles/34073/Inter-Process-Communication-IPC-Introduction-and-S

How to launch a browser and later direct it to a page?

I need to launch a browser, do some work and then make the browser navigate to a URL (in that order).
The first part is of course simple and I have a Process object. I am at a loss as to how to later direct it to the target page?
How do I treat the Process as a browser and make it navigate to the desired page?
Any help, pointers, code snippets appreciated.
Instead of launching the browser & then navigating to the page, just tell the OS that you want to run the URL. Windows will pick the correct browser, and navigate the user to the given URL.
System.Diagnostics.Process.Start("http://www.StackOverflow.com");
If you don't need to do this in production, you could use a testing library such as WatiN to do this:
using WatiN.Core;
//Placeholder page to launch initial browser
IE ie = new IE("http://www.google.com");
DoSomeWork();
//Now navigate to the page you want
ie.GoTo("http://stackoverflow.com");
My first instinct for this question was DDE, but it appears that has been decommissioned in Windows Vista so that is no good. Shame, as it was the only consistent mechanism in Windows for Interprocess Communication (IPC)...oh how I miss Arexx on the Amiga.
Anyhow, I believe the following will work but unfortunately, due to the way it works, it launches Internet Explorer irrespective of the configured browser.
If your application has a Form, then create a WebBrowser control on it. Set this to non-visible as we are only making use of its as a launching device rather than to display the web page.
In code, at the point where you want to show a web page, use the following code:
webBrowser1.DocumentText = "window.open('How to launch a browser and later direct it to a page?', 'BananasAreOhSoYummy');";
What this does is to tell the WebBrowser control, which is just the IE in disguise, to open a new window called 'BananasAreOhSoYummy'. Because we have given the window a name, we can use that line repeatedly, with different URLs, to change the page in that particular browser window. (A new window will be opened if the user has happened to close it.)
I will have a think about an approach that honours the user's default browser choice.
If you don't need the actual instance of IE, you can use the System.Windows.Forms.WebBrowser control.
I think instead of sending the browser a url you could send it javascript that would run and direct the browser to a site.
Not sure if this would work but I see no reason why it wouldn't

asp.net: data put in session is available while working in internet explorer but not in opera

it's a really weird thing - i have a website that works perfectly in maxthon (internet explorer based browser). i started it in opera and found out that the data put in the Session dictionary on one site, is not available on the other... i mean i have Welcome.aspx, where when you click next the following code is executed:
Session["sessionData"] = sessionData;
Response.Redirect("~/Models.aspx");
while debugging i can see that in models.aspx Session in empty when executing in opera but everything is fine when executing in maxthon.
has anyone got any idea what can be wrong? because i'm clueless.. is this some opera preferences thing or is it something in code?
Edit: i checked Session.IsNewSession and while executing in maxthon isnewsession is set to false but in opera it's true. it seems that in opera when moving to a new page it somehow creates new session...
If your write the session on the first hit then you should do
Response.Redirect("nextpage.asp", false);
Otherwise it wont write the whole responsestream and the cookie might not have been written. You can instead choose to have cookiless sessions. But then your open to session hijacking.
ASP.NET session is stored by a key that is saved as a cookie in the browser. Check Opera to see if it is accepting cookies - it will need to in order for ASP.NET session to work properly.
Maybe you have cookies disabled in Opera. Session works (unless speciefies as cookieless in web.config) by storing an identifier in a cookie. If the brwoser does not allow the access, the server won't be able to locate the data

Categories

Resources