我使用 Windows.Web.Http.HttpClient 连接到我的 WEB API。应用程序没有提示输入用户 ID 和密码,但最近我通过将 AuthorizeAttribute 过滤器从操作级别移动到类级别来更改 WEB API。现在我的 Windows 应用商店 8.1 应用程序提示输入用户 ID 和密码。请让我知道如何设置 HttpClient 不提示登录名和密码。任何人都可以建议我是否需要向我的 httpcleint 添加标头
使用 (Windows.Web.Http.HttpClient httpClient = new Windows.Web.Http.HttpClient())
{
// 添加用户代理标头
var headers = httpClient.DefaultRequestHeaders;
// 检查用户标头值的安全方法是 TryParseAdd 方法
// 因为我们知道这个 header 没问题,所以我们使用 ParseAdd 会抛出异常
// 值不好 -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)))
我没有找到发送默认凭据的方法。
使用禁用 UI 对话框HttpBaseProtocolFilter.AllowUI
。尝试这个:
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
。尝试这个:
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 凭据(域凭据)?只需添加企业认证能力给你的包.appxmanifest.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)