Is there a way to pass credential to WebBrowser Control? - c#

Currently i have a winform where i need to open report in WebBrowser Control. I am using impersonation method to view as different user. But somehow the WebBrowser will pop up a windows security authentication for me to enter my credential. When i input the credential as someone that has the right to view the report (not my credential) it will just show a blank page. Not even a message that says:
The permissions granted to user 'Domain\first.last' are insufficient for performing this operation. (rsAccessDenied)
The First Page:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (new ImpersonateUser("TestUsername", Environment.UserDomainName, "TestPassword"))
{
Form2 reportForm = new Form2();
reportForm.Text = "Test";
reportForm.GetReportUrl("http://url/");
reportForm.ShowDialog();
}
}
}
The Second Page:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
label1.Text = Environment.UserName;
}
private void Form2_Load(object sender, EventArgs e)
{
webBrowser1.Refresh();
}
public void GetReportUrl(string repUrl)
{
webBrowser1.Url = new Uri(repUrl);
}
}
Can someone explains to me why is this happening? And how i can fix this? Thank you.

But i didn't get why you have to do impersonation to launch a form, from the above code you are not doing any by doing impersonation.
if you are facing the problem in launching a application under user profile then the below link will be helpful.
WTSQueryUserToken returning FALSE

Related

Toggle and communicate between two Windows forms in Visual C#?

I have two forms in my Windows Form Application, a main and a login info form.
The main form has a button which opens the login info form on being clicked. The login info form gets displayed as a new form on the main window with the main window also being visible in background while the user enters login info. If the login info is correct the login form should close while the main form should display the string "Welcome" and the correct username from login form. Now the issue is that after entering the correct login info on the login form and clicking the Login button, a new window of the main form appears with the previous main form also running as shown in the figure..
How can I make the previous instance of main form close and get the correct text displayed? Any help is appreciated.
I am using the following codes for creating and opening the main and the login info forms.
Code snippet of main form:
public partial class Main_Form : Form
{
private string u_name = "";
public Main_Form(string username)
{
InitializeComponent();
u_name = username;
label1.Text = label1.Text + " " + u_name;
}
private void Loginbutton_Click(object sender, EventArgs e)
{
Form Form2 = new login_Form();
Form2.ShowDialog();
}
}
Code snippet of login form:
public partial class login_Form : Form
{
public static string username = "";
public login_Form()
{
InitializeComponent();
}
private void Button_Login_Click(object sender, EventArgs e)
{
if (textBox_Login.Text == "Admin" && textBox_Password.Text == "123")
{
this.Hide();
username = textBox_Login.Text;
Form Form1 = new Main_Form(username);
Form1.ShowDialog();
}
else
{
MessageBox.Show("Please Enter Username and/or Password Again!");
}
}
}
You shouldn't have 2 instances of MainForm, just use one and update as required.
In your mainform you should call the LoginForm and check the DialogResult (I will explain this later).
So here, if you get an OK result it means the Login worked, otherwise the user cancelled.
So in MainForm change the constructor to be empty and remove the u_name parameter
public Main_Form()
{
....
}
private void Loginbutton_Click(object sender, EventArgs e)
{
Form Form2 = new login_Form();
if(Form2.ShowDialog() == DialogResult.OK) //We call LoginForm and wait for a Result
{
//Login was successful. Then we can set the label to the username, which is now a property for Login Form (see below)
label1.Text = Form2.UserName;
}
else
{
//Login was cancelled
//You must now implement logic to handle this case
}
}
Now in the Login form you want to check login and return the dialog result to the MainForm. DialogResult is, as the name suggests, the result of calling the LoginForm Dialog.
You will also need another click event for the cancel button.
public partial class login_Form : Form
{
//username is changed to a property
public string Username { get; private set; }
public login_Form()
{
InitializeComponent();
}
private void Button_Login_Click(object sender, EventArgs e)
{
//The check remains as before.
if (textBox_Login.Text == "Admin" && textBox_Password.Text == "123")
{
//If successful, we set the dialogresult, username and close this form.
this.Username = textBox_Login.Text;
this.DialogResult = DialogResult.OK;
this.Close();
}
else
{
//Otherwise we handle the error
MessageBox.Show("Please Enter Username and/or Password Again!");
}
}
//The new click event for the cancel button
private void Button_Login_Cancel(object sender, EventArgs e)
{
//We set the dialogresult to cancel (could be anything else) and close
this.DialogResult = DialogResult.Cancel;
this.Close();
}
}

Web Browser will not navigate to link

My WebBrowser object will not Navigate() to the link I give it.
The object remains empty when I run it.
When I put the url into the url property though, it successfully loads the link.
namespace Program
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
webbMain.Navigate("https://twitter.com/login");
}
}
}
You can try the following:
You can try the following in Page_Load:
WebBrowser wbbMain= new WebBrowser();
wbbMain.AllowNavigation = true;
wbbMain.Navigate("http://www.stackoverflow.com");
Also, you can try to provide override for DocumentCompleted event.
If nothing works, then see if Internet explorer is isntalled. If its not installed then web brower may not work.

Returning to the last form the user was on

I have multiple forms and i can make them all go to a certain form through the click of one button, but how do I give one button the ability to go back to the last page the user was on, and not just the form before the current one? This is for my 'help' form, as users can visit the form through many other forms. My coding was as follows. I am using Winforms
namespace Spanish_Quiz
{
public partial class Help : Form
{
public Help()
{
InitializeComponent();
CenterToScreen();
}
private void btnBackToStart_Click(object sender, EventArgs e)
{
Homescreen Homescreen = new Homescreen();
Homescreen.Show();
Homescreen.Activate();
this.Hide();
}
private void btnBack_Click(object sender, EventArgs e)
{
/*This is where my problem is,
what code do I write here that could be similar to
my 'back to start' button's code?*/
}
}
}

When Form1 loads, hide it and show Form2

Been stuck for quite a while reading similar posts here, I did find a solution but it was in dummy code and I just don't know what I'm doing wrong.
I have 2 forms, when the main form loads up I want to hide it and show form2 (the login form)
code looks like this.
private void Form1_Load(object sender, EventArgs e)
{
login loginform = new login();
loginform.Show();
this.Hide();
}
But when I run the program both forms are open and visible.
What am I doing wrong? Shouldn't the main form be hidden?
The Hide method does not have any effect from the Load event, since there isn't a handle created yet.
You have two options:
Using the Shown event (or better, the HandleCreated event) and hide it if a condition is met (like a variable 'logon form not shown')
Show the logon form as start form, then open the 'main' form. You can do this by passing an ApplicationContext around and pass on control to the main form.
You can do it with help of owner property, here is working example
Main form
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
var loginFormMax = new LoginFormMax { Owner = this };//save main form as owner inside child form
loginFormMax.Show();
}
}
Child Form
public partial class LoginFormMax : Form
{
public LoginFormMax()
{
InitializeComponent();
}
private void LoginFormMax_Shown(object sender, EventArgs e)
{
var owner = this.Owner;
owner.Hide();//now you have control over owner form, just hide it
}
private void LoginFormMax_FormClosing(object sender, FormClosingEventArgs e)
{
var owner = this.Owner;
owner.Show();//now you have control over owner form, just show it again
}
}

winform webbrowser not replaced

From my Form1 I initialize a class scraper. In the scraper class is an function login. The idea is that that class log's the user in on an website, and returned the web browser so that an logged in webbrowser control is available in Form1.
I've got this code so far: Form1
private void button1_Click(object sender, EventArgs e)
{
Scraper scraper = new Scraper(this);
scraper.login(conf._webLogin);
}
public void updateLoginWeb(WebBrowser web)
{
webBrowser1 = web;
MessageBox.Show("DONE");
}
The conf class:
public WebBrowser _webLogin = new WebBrowser();
The scraper class:
private Form1 parent;
private WebBrowser _web_Login = new WebBrowser();
public Scraper()
{
}
public Scraper(Form1 parent)
: this()
{
this.parent = parent;
}
public void login(WebBrowser web)
{
_web_Login = web;
_web_Login.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(login_DocumentCompleted);
_web_Login.Navigate("http://www.google.com/");
}
private void login_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//This line is so you only do the event once
if (e.Url != _web_Login.Url)
return;
parent.updateLoginWeb(_web_Login);
}
I use google as test, but nothing works (not even an another site).
The problem is that the webbrowser in the Form isn't updated. It's still an white screen.
What do you guys think of this? Do you know what the problem is or do you guys know an better way to handle this?
I think your problem is that you can not simply assign the webBrowser variable:
webBrowser1 = web;
You are changing the Form1.webBrower1 variable, but the Forms.Controls collection is still pointing to the original webBrowser control.
Can't you just pass Form1.webBrower1 to scraper.login function?:
private void button1_Click(object sender, EventArgs e)
{
Scraper scraper = new Scraper(this);
scraper.login(webBrowser1);
}
public void updateLoginWeb(WebBrowser web)
{
//webBrowser1 = web; // you don't need this anymore
MessageBox.Show("DONE");
}
If you really need to replace your control you can do something like:
public void updateLoginWeb(WebBrowser web)
{
Controls.Remove(webBrowser1);
Controls.Add(web);
webBrowser1 = web; // you don't need this anymore
MessageBox.Show("DONE");
}
But you will probably to set the new webbrowser layout properties manually.

Categories