本文介绍了Windows.Web.Http.HttpClient + WEB API Windows身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Windows.Web.Http.HttpClient连接到我的WEB API.应用程序尚未提示输入用户名和密码,但是最近我通过将AuthorizeAttribute过滤器从Action移到Class级别来更改了WEB API.现在,我的Windows store 8.1应用程序提示您输入用户名和密码.请让我知道如何将HttpClient设置为不提示登录名和密码.any1可以建议我是否需要在我的httpcleint中添加标头

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

使用(Windows.Web.Http.HttpClient httpClient =新的Windows.Web.Http.HttpClient()){//添加用户代理标头var headers = httpClient.DefaultRequestHeaders;

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

//从用户检查标头值的安全方法是TryParseAdd方法//由于我们知道此标头是可以的,因此我们将ParseAdd与一起使用将抛出异常//值错误- http://msdn.microsoft.com/en-us/library/windows/apps/dn440594.aspx

// 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.

推荐答案

使用 HttpBaseProtocolFilter.AllowUI 禁用UI对话框.试试这个:

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

您需要凭证吗?使用 HttpBaseProtocolFilter.ServerCredential .试试这个:

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

您是否需要默认的Windows凭据(域凭据)?只需将企业身份验证功能添加到 Package.appxmanifest .

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

这篇关于Windows.Web.Http.HttpClient + WEB API Windows身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 08:06
查看更多