Windows.Web.Http.HttpClient + WEB API Authentication

Im using Windows.Web.Http.HttpClient to connect to my WEB API. The application did not request a user ID and password, but I recently changed the WEB API by moving the AuthorizeAttribute filter from Action to class. My Windows application now stores the application 8.1 request for the user ID and password. Please let me know how to install HttpClient so as not to ask for a username and password. Can any1 suggest me I need to add a header to my httpcleint

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

// A safe way to check the value of the header for the user is the TryParseAdd method // Since we know that this header is OK, we use ParseAdd with 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 don't see a way to send default credentials.

+3
source share
2 answers

Disable user interface dialogs with HttpBaseProtocolFilter.AllowUI. Try the following:

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 the following:

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

Windows ( )? Enterprise Authentication Package.appxmanifest.

+12

, , , , , , . Windows, Enterprise Authentification UWP.

, :

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

401.3...

/ ServerCredential, :

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

, Enterprise Authentication , ...

0

All Articles