您的位置:首页 > 移动开发

Passing Domain Credentials in C# to a WebBrowser Control in .Net WinForms Applications(winform中传递域认)

2017-02-09 17:15 3459 查看
https://ericniemiec.wordpress.com/2012/07/24/passing-domain-credentials-in-c-to-a-webbrowser-control-in-net-winforms-applications/

I have been trying to embed a WebBrowser control in a custom WinForms application that points to a secured web application running in IIS for some time now and have finally found a solution that works.  There are plenty of posts on the internet that  pass an
authorization header in to the Navigate method of the WebBrowser control but I only found one that combines that technique with adding the username and password to the URL.  Without adding the second technique, the initial page loads without prompting for
credentials but when you click any link on that page, it then prompts you for credentials again.

The combination of both techniques solved my problem but is not very secure.  Our web application is accessed via SSL but passing the username and password in the URL is not recommended.  In our case, the application is secured, but not a major risk if compromised
– this is not always the case so be sure to understand your security needs before implementing this code.  You only need to do it this way if using the WebBrowser control, if you were to use the WebClient or HttpWebRequest objects, you would be able to use
the NetworkCredential object to pass in the credentials – which we use in other parts of the same application or when passing permissions to web service calls (which I do here: https://ericniemiec.wordpress.com/2009/02/20/programmatically-add-credentials-to-an-asmx-web-service-that-is-password-protected-in-c/ ).

In my case, the web server is running IIS 7.5, and is setup with Basic authentication (no Windows Authentication is enabled), a default domain and realm is set and a SSL certificate is loaded for this site.

Here is the code to make this all work, even though the header says ‘Basic’, this works for passing domain credentials as long as the default domain is set in the Basic Authentication dialog in IIS.

Even though I had used this technique years ago, it was not until I saw this post that it hit me:

http://stackoverflow.com/questions/6042829/how-can-i-open-a-url-in-web-browser-such-as-ie-and-pass-credentials

Hope this helps!

-Eric Niemiec
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐