【问题标题】:Windows.Web.Http.HttpClient + WEB API Windows AuthenticationWindows.Web.Http.HttpClient + WEB API Windows 身份验证
【发布时间】:2014-02-09 20:05:16
【问题描述】:

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

使用 (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)))

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

【问题讨论】:

    标签: asp.net-web-api windows-store-apps windows-8.1 winrt-httpclient


    【解决方案1】:

    使用 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 凭据(域凭据)吗?只需将 Enterprise Authentication 功能添加到您的 Package.appxmanifest

    【讨论】:

      【解决方案2】:

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

      如果我将登录名/密码添加到 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);
      

      但是我看不出这种情况下企业认证能力的作用是什么,如果我需要通过登录名和密码...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-04-06
        • 2017-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-07
        • 2017-10-22
        • 1970-01-01
        相关资源
        最近更新 更多