1

I am trying to view report using someone else's credential. I am stuck on how to assign credential and then navigate to the URL.

Question: Is it possible to assign credential to WebRequest then navigate to the url? Something like this maybe:

WebRequest request = WebRequest.Create(repUrl);
request.Credentials = new NetworkCredential("UserName", "Password");
webBrowser1.Navigate(request.RequestUri);

I know i am able to see the report when i use something like this. But i cannot click the report:

WebRequest request = WebRequest.Create(repUrl);
request.Credentials = new NetworkCredential("Username", "Password");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader receiveStream = new StreamReader(response.GetResponseStream());
webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.DocumentStream = receiveStream.BaseStream;

Please help me on this. I stuck at this problem for too long now. Thank you.

Prix
  • 19,417
  • 15
  • 73
  • 132
MRu
  • 1,225
  • 7
  • 20
  • 44
  • 1
    Depends on whether its a login within the page of the website or an actual authentication request, if its an authentication of type basic, digest, NTLM, or Kerberos then yes, if its not then you would have to call the login page first with the proper post/get parameters. – Prix Mar 28 '19 at 07:43
  • It is a windows credentials. Sorry i did not mention this before. – MRu Mar 28 '19 at 08:13
  • Should work if that really is the case, this might help https://stackoverflow.com/a/1829634/342740 – Prix Mar 28 '19 at 10:18

1 Answers1

0

Setting credentials like below

request.Credentials = new NetworkCredential("UserName", "Password");

would be possible if a page accepts credentials (basic authentication), that to my experience in most cases it's not like that.

If you want to use WebRequest, you would need to supply headers, cookies, and you would need a cookie container, now how to get cookies is another story.

As you are using WebBrowser control. the easiest way that I would suggest is that you navigate to login page first, fill the credentials and click on submit button. then navigate to the page page you want:

private void webbrowser1_DocumentCompleted(object sender, DocumentCompletedEventArgs e)
{
     if(webbrowser1.Url.EndsWith("login.aspx")  //check if it is the login url
     {
          var doc = webbrowser1.Document;
          doc.GetElementById("email").SetAttribute("value", email);
          doc.GetElementById("password").SetAttribute("value", password);
          doc.GetElementsByTagName("input").OfType<HtmlElement>()
               .FirstOrDefault(x => x.GetAttribute("type") == "submit"))
               .InvokeMember("click");
     }
     else
     {
           webbrowser1.Navigate(repUrl);
     }


}

In the above answer, I have just assumed that login page is login.aspx and email and password fields have id of email and password, of course you have to change how you fill these inputs with thier own ids or if they don't have an Id using their names, ...

Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
  • Sorry, i did not mention this in my question. I am using windows authentication. Is there a way to pass this? Since when i navigate to the report it still says that i log in as my current user. Not the user that i have set in new NetworkCredential("UserName", "Password"); – MRu Mar 28 '19 at 08:13
  • @MRu then it's not on the internet right? it's on a local network? – Ashkan Mobayen Khiabani Mar 28 '19 at 08:15
  • Yes. Is it not possible? – MRu Mar 28 '19 at 08:34
  • Basically the URL is just for me to see a specific SSRS report. But only one user has access to view the report. So that is why i need to pass that user credential when navigating to the URL. – MRu Mar 28 '19 at 08:44