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



Im 使用 Windows.Web.Http.HttpClient 连接到我的 WEB API。应用程序没有提示输入用户ID和密码,但最近我通过将AuthorizeAttribute过滤器从操作移动到类级别来更改WEB API。现在我的Windows商店8.1应用程序提示输入用户ID和密码。请告诉我如何设置HttpClient不提示登录名和密码。any1 可以建议我是否需要将标头添加到我的 httpcleint 中

吗?using (Windows.Web.Http.HttpClient

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

检查用户标头值的安全方法是 TryParseAdd 方法 由于我们知道这个标头是可以的,我们使用 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)))

我没有看到发送默认凭据的方法。

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

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 凭据(域凭据)?只需将企业身份验证功能添加到 Package.appxmanifest

我试图应用您的解决方案,但它没有按预期工作,或者我不明白我应该做什么。我需要使用 Windows 凭据,并且已在 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 错误...

如果我将登录名/密码添加到服务器凭据,这效果很好:

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

但是如果我需要传递登录名和密码,我看不到在这种情况下企业身份验证功能的作用是什么......

最新更新