providing domain/user credentials to webview control

坚强是说给别人听的谎言 提交于 2019-11-30 18:24:59

问题


Does anyone know how to provide credentials to a WebView control (or even better - run a Windows 8 Metro style application / WinRT app in the context of a specific domain user?) I've tried following the HttpClient + WebView approach as listed here (http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/05e46a0a-e913-469d-a4a5-4a805dcf158a) but it still prompts the user for credentials, since in the returned content there are links to secured items.

I have a NetworkCredential object, and I pretty much want to apply it to every single HTTP call that gets made.

Surely someone must have done this before? :)


回答1:


In order to connect to sites on your corporate network you must enable the privateNetworkClientServer capability in your Package.appxmanifest. You can do this in the XML or you can double-click on the file in Visual Studio and use the designer (capabilities tab). In the UI it's called "Home or Work Networking".

Now that you have access to the intranet you can navigate to the site but you will be prompted to provide credentials. If you want the users domain identity to automatically be supplied (enterprise single sign on) you need to enable the enterpriseAuthentication capability as well. This is called "Enterprise Authentication" in the manifest UI.

From there you should be able to browse to any intranet site using a WebView and not have to authenticate. If you are looking to do things outside of the WebView, you should check out the following articles and samples:

Using WebAuthenticationBroker:

http://code.msdn.microsoft.com/windowsapps/Web-Authentication-d0485122

WebAuthenticationBroker has an option called UseCorporateNetwork. Make sure to use that when following along in the sample above.

Finally, if you want to do Single Sign On with other sites like Facebook or Flicr, see this sample:

http://msdn.microsoft.com/en-US/library/windows/apps/xaml/Hh465283




回答2:


I have came across same problem, but luckily found an answer :) Main problem in here is that Windows store applications contain 2 diffrent HttpClient's

One of them is "classic" one we know from c# apps (used automatically) and the other one is "new" HttpClient - which is connected with WebView :)

Bellow are both types : System.Net.Http.HttpClient ( classic one ) Windows.Web.Http.HttpClient ( new one )

So remember to declare the new One and do something like the code bellow

var filter = new HttpBaseProtocolFilter();    
filter.ServerCredential = new Windows.Security.Credentials.PasswordCredential("http://website","login", "password");
            Windows.Web.Http.HttpClient client2 = new Windows.Web.Http.HttpClient(filter);
            var response = await client2.GetAsync(new Uri("http://website"));
 WebView.Source = new Uri("http://website");

Now remember to change login and password to credentials you want to use, and a website is a site you want to authenticate to.

It is important to get the response from server - this will make user authenticated @ server so next time you go with webView to that site you will be authenticated

It works fine with basic authentication and NTLM authentication

In this cae scenario remember to have enterpriseAuthentication turned off

Hope it will help people searching for solution of this problem :)



来源:https://stackoverflow.com/questions/11181821/providing-domain-user-credentials-to-webview-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!