providing domain/user credentials to webview control

后端 未结 2 907
情歌与酒
情歌与酒 2021-01-06 08:46

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 us

2条回答
  •  半阙折子戏
    2021-01-06 09:30

    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 :)

提交回复
热议问题