Application that gets URLs with the default browser credentials

D

dk9

The Situation:
I'm working in my browser at a secured site (https).
At the same time I need my application to get and parse some urls from
that same site.

What I've tried:
I've login with my browser, but when trying to get the pages using
WebRequest from my application I can only get the "you need to login
page".
I've then put a WebBrowser control in my application and do the login
from there. I can get the urls and navigate the site but I cannot
fully work from there because I need pop-ups pages, and those are
opened in the default browser (in these case showing the "you need to
login page").

My Questions:
What approches can I make from here?
What shall I use so my application have the same credentials of my
browser or vice-versa?
Webbrowser, WebRequest, WebClient??? any other?


Many Thanks for any help
 
D

dk9

Resolved using two WebBrowser controls and opening the pop-ups pages
in the second WebBrowser
 
P

pedrito

Sorry, I never saw your original post. You can also get the cookies from the
installed browser and use those. It would assume they've already
authenticated through the browser and that the cookies are still valid.

I have a small library on SourceForge for reading cookie files from IE and
Firefox:
http://ncookiereader.sourceforge.net/
 
U

UL-Tomten

I have a small library on SourceForge for reading cookie files from IE and
Firefox:http://ncookiereader.sourceforge.net/
throw new NotImplementedException("No support for finding the cookie path yet.");

For what it's worth, here's what I did once to find the Firefox cookie
path (using the default profile of the current user):

private const string PATH_PROFILES_INI = @"%APPDATA%\Mozilla\Firefox
\profiles.ini";
private const string PATH_COOKIES_TXT = @"%APPDATA%\Mozilla\Firefox\
%PROFILE%\cookies.txt";
....
string pathAppData =
Environment.GetEnvironmentVariable("APPDATA");
string profilesIniPath = PATH_PROFILES_INI.Replace("%APPDATA%",
pathAppData);
string profileDir = null;
// Find default profile in profiles.ini
Dictionary<string, string>[] firefoxProfilesIni =
ParseIniFile(profilesIniPath);
foreach (Dictionary<string, string> iniGroup in
firefoxProfilesIni) {
if (iniGroup.ContainsKey("Default") && iniGroup["Default"] ==
"1")
profileDir = iniGroup["Path"];
}
_cookiePath = PATH_COOKIES_TXT.Replace("%PROFILE%",
profileDir).Replace("%APPDATA%", pathAppData);
....
const string INI_PATTERN = @"\s*\[([^\]]*)\]\r?\n([^\[]*)";
public static Dictionary<string, string>[] ParseIniFile(string
IniFilePath) {
List<Dictionary<string, string>> dictList = new
List<Dictionary<string, string>>();
string iniContents = File.ReadAllText(IniFilePath);
MatchCollection mc = Regex.Matches(iniContents, INI_PATTERN,
RegexOptions.Singleline);
foreach (Match m in mc) {
Dictionary<string, string> dict = new Dictionary<string,
string>();
string iniGroupName = m.Groups[1].Value.Trim();
dict.Add("_name", iniGroupName);
string[] newlines = { "\n", "\r", "\r\n" };
foreach (string keyvalue in m.Groups[2].Value.Split(newlines,
StringSplitOptions.RemoveEmptyEntries)) {
if (keyvalue.Contains("=")) {
dict.Add(keyvalue.Split('=')[0].Trim(),
keyvalue.Split('=')[1].Trim());
}
}
dictList.Add(dict);
}
return dictList.ToArray();
}
 
P

pedrito

UL-Tomten said:
I have a small library on SourceForge for reading cookie files from IE
and
Firefox:http://ncookiereader.sourceforge.net/
throw new NotImplementedException("No support for finding the cookie path
yet.");

For what it's worth, here's what I did once to find the Firefox cookie
path (using the default profile of the current user):

private const string PATH_PROFILES_INI = @"%APPDATA%\Mozilla\Firefox
\profiles.ini";
private const string PATH_COOKIES_TXT = @"%APPDATA%\Mozilla\Firefox\
%PROFILE%\cookies.txt";
...
string pathAppData =
Environment.GetEnvironmentVariable("APPDATA");
string profilesIniPath = PATH_PROFILES_INI.Replace("%APPDATA%",
pathAppData);
string profileDir = null;
// Find default profile in profiles.ini
Dictionary<string, string>[] firefoxProfilesIni =
ParseIniFile(profilesIniPath);
foreach (Dictionary<string, string> iniGroup in
firefoxProfilesIni) {
if (iniGroup.ContainsKey("Default") && iniGroup["Default"] ==
"1")
profileDir = iniGroup["Path"];
}
_cookiePath = PATH_COOKIES_TXT.Replace("%PROFILE%",
profileDir).Replace("%APPDATA%", pathAppData);
...
const string INI_PATTERN = @"\s*\[([^\]]*)\]\r?\n([^\[]*)";
public static Dictionary<string, string>[] ParseIniFile(string
IniFilePath) {
List<Dictionary<string, string>> dictList = new
List<Dictionary<string, string>>();
string iniContents = File.ReadAllText(IniFilePath);
MatchCollection mc = Regex.Matches(iniContents, INI_PATTERN,
RegexOptions.Singleline);
foreach (Match m in mc) {
Dictionary<string, string> dict = new Dictionary<string,
string>();
string iniGroupName = m.Groups[1].Value.Trim();
dict.Add("_name", iniGroupName);
string[] newlines = { "\n", "\r", "\r\n" };
foreach (string keyvalue in m.Groups[2].Value.Split(newlines,
StringSplitOptions.RemoveEmptyEntries)) {
if (keyvalue.Contains("=")) {
dict.Add(keyvalue.Split('=')[0].Trim(),
keyvalue.Split('=')[1].Trim());
}
}
dictList.Add(dict);
}
return dictList.ToArray();
}

Thanks. I actually know how to do it, just haven't had time to implement it
yet. I have another project that's consumed most of my free time lately.
nCookieReader was a library I originally created as part of the other
project, but thought others might find it useful. The other app is launched
from the browser and the cookie path for firefox is passed as a parameter,
so that's why I didn't bother implementing it at the time. I plan to add it,
however, when I get free time... Somewhere between this other app, work,
school and research.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top