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
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.
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
相关文章推荐
- Simple way to expose a .NET WinForm control as an ActiveX control in any HTML page
- (C#) A demo on how to auto fill out the "account" value in log in web page in "WebBrowser" control.
- How to Make Dynamic Hyperlinks Using C# in ASP.NET
- "HOW TO: Create a Smooth Progress Bar in Visual C# .NET"的学习笔记
- 在网页中使用Winform控件,并调整其大小(using Winform control in ASP.NET and adjust its size)
- How to show separators in WinForm's StatusStrip control?
- How To Implement Forms-Based Authentication in Your ASP.NET Application by Using C# .NET
- 期待已久的一本电子书出来了:Applying Domain-Driven Design and Patterns: With Examples in C# and .NET
- Beginning ASP.NET 3.5 in C# 2008 From Novice to Professional Second Edition(ASP.NET 3.5(C#2008)入门 )翻译-第十章(2)
- A Programmers Guide to ADO.NET in C#
- C# .Net 2.0实例学习:WebBrowser页面与WinForm交互技巧(一)
- overrid events of Webbrowser ActiveX Control in .Net
- How to read and save Images in a Sql Server Database using ADO.NET and C#
- How to host winform control in IE
- Beginning ASP.NET 3.5 in C# 2008 From Novice to Professional Second Edition(ASP.NET 3.5(C#2008)入门 )翻译-第十章(3)
- HOW TO: Set a Windows Hook in Visual C# .NET
- How to debug Control Panel applet (cpl) in VS .NET 2005
- Unhandled exceptions cause ASP.NET-based applications to unexpectedly quit in the .NET Framework 2.0
- How to programmatically assign a SkinID to a control while using a master page in ASP.net 2.0
- Beginning ASP.NET 2.0 in C# 2005: From Novice to Professional (Beginning: from Novice to Professiona