Windows.Web.Http.HttpClient + WEB API Windows Authentication

只愿长相守 提交于 2019-12-30 10:39:07

问题


Im using Windows.Web.Http.HttpClient to connect to my WEB API. Application haven't prompted for userid and password, but recently i changed WEB API by moving AuthorizeAttribute filter from Action to Class level. Now my Windows store 8.1 application prompt for user id and password. Please let me know how to set HttpClient to not prompt the login and password. Can any1 suggest me do i need to add header to my httpcleint

using (Windows.Web.Http.HttpClient httpClient = new Windows.Web.Http.HttpClient()) { // Add a user-agent header var headers = httpClient.DefaultRequestHeaders;

// The safe way to check a header value from the user is the TryParseAdd method // Since we know this header is okay, we use ParseAdd with will throw an exception // with a bad value - http://msdn.microsoft.com/en-us/library/windows/apps/dn440594.aspx

                headers.UserAgent.ParseAdd("ie");
                headers.UserAgent.ParseAdd("Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");


                using (var response = await httpClient.GetAsync(new Uri(url)))

I dont see a way to send Default credentials.


回答1:


Disable UI dialogs using HttpBaseProtocolFilter.AllowUI. Try this:

Windows.Web.Http.Filters.HttpBaseProtocolFilter filter =
    new Windows.Web.Http.Filters.HttpBaseProtocolFilter();
filter.AllowUI = false;
HttpClient client = new HttpClient(filter);

Uri uri = new Uri("http://localhost/?basic=1");
var response = await client.GetAsync(uri);
System.Diagnostics.Debug.WriteLine(response);

Do you need credentials? Use HttpBaseProtocolFilter.ServerCredential. Try this:

Uri uri = new Uri("http://localhost?ntlm=1");

Windows.Web.Http.Filters.HttpBaseProtocolFilter filter =
    new Windows.Web.Http.Filters.HttpBaseProtocolFilter();
filter.AllowUI = false;

// Set credentials that will be sent to the server.
filter.ServerCredential =
    new Windows.Security.Credentials.PasswordCredential(
        uri.ToString(),
        "userName",
        "abracadabra");

HttpClient client = new HttpClient(filter);
var response = await client.GetAsync(uri);
System.Diagnostics.Debug.WriteLine(response);

Do you need default Windows credentials (domain credentials)? Simply add the Enterprise Authentication capability to your Package.appxmanifest.




回答2:


I tried to apply your solution but it doesn't work as expected, or maybe I don't understand what I'm supposed to do. I need to user the Windows credentials and I have enabled the Enterprise Authentification capability on my UWP app.

I use the code that you suggest:

var filter = new HttpBaseProtocolFilter();
filter.AllowUI = false;
var client = new HttpClient(filter);
HttpResponseMessage response = new HttpResponseMessage();
response = await client.PostAsync(concUri, null);

But the response returns me a 401.3 error...

If I add the login/password to the ServerCredential, this works well:

var filter = new HttpBaseProtocolFilter();
filter.AllowUI = false;
filter.ServerCredential  = new Windows.Security.Credentials.PasswordCredential(WebServiceConstants.WebServiceUrl.ToString(), "login", "password");
var client = new HttpClient(filter);
HttpResponseMessage response = new HttpResponseMessage();
response = await client.GetAsync(concUri);

But I don't see what is the role of the Enterprise Authentication capability in this case, if I need to pass the login and the password...



来源:https://stackoverflow.com/questions/21664313/windows-web-http-httpclient-web-api-windows-authentication

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