【问题标题】:Passing current authentication credentials from UWP app to web service to access resources on server with these credentials将当前身份验证凭据从 UWP 应用程序传递到 Web 服务,以使用这些凭据访问服务器上的资源
【发布时间】:2018-08-31 04:58:41
【问题描述】:

我的 Windows 10 UWP 应用正在调用我创建的 WebAPI Web 服务。在调用 Web 服务时,我需要在客户端传递当前凭据,以便它可以使用这些凭据访问其他资源。

我还需要在不提示用户输入凭据的情况下执行此操作,以便无缝体验。

我可以使用System.Net.Http 来做到这一点,并成功地将当前凭据传递给服务器以用于访问资源。这会发送请求并在没有任何提示的情况下返回响应。我在 UWP 应用上启用了 Enterprise AuthenticationPrivate Networks 功能来完成这项工作。

问题:这对 GET 请求有效,但不适用于对同一服务器的 POST 请求。 POST 请求导致以下错误:

这个 IRandomAccessStream 不支持 GetInputStreamAt 方法 因为它需要克隆,而这个流不支持克隆。

我读到这是此链接上的一个错误:PostAsync throwing IRandomAccessStream error when targeting windows 10 UWP。针对此错误在多个位置提出的解决方法是改用Windows.Web.Http。但是,如果这样做,如何将默认/当前凭据传递给服务器?

这是我用来使用当前 Windows 凭据执行 GET 请求而不提示输入的代码。它完美无缺:

System.Net.Http.HttpClientHandler handler = new System.Net.Http.HttpClientHandler
{
    UseDefaultCredentials = true
    // Credentials = (NetworkCredential)System.Net.CredentialCache.DefaultNetworkCredentials
    //using either one of the above enables me to have the web service use the current credentials without prompting
};

string responseContent = string.Empty;

using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient(handler))
{
    System.Net.Http.HttpRequestMessage requestMessage = new System.Net.Http.HttpRequestMessage();

    requestMessage = new System.Net.Http.HttpRequestMessage
    {
        Method = System.Net.Http.HttpMethod.Get,
        RequestUri = new Uri(strWebServiceURL)

    };

    using (System.Net.Http.HttpResponseMessage response = await client.SendAsync(requestMessage))
    {

        responseContent = await response.Content.ReadAsStringAsync();

    }

    //This also works fine
    using (System.Net.Http.HttpResponseMessage response = await client.GetAsync(strWebServiceURL))
    {

        responseContent = await response.Content.ReadAsStringAsync();

    }

下面是我用来执行 POST 请求的代码,它会导致 IRandomAccessStream 错误:

System.Net.Http.HttpClientHandler handler = new System.Net.Http.HttpClientHandler
{
    UseDefaultCredentials = true
   // Credentials = (NetworkCredential)System.Net.CredentialCache.DefaultNetworkCredentials
   //using either one of the above enables me to have the web service use the current credentials without prompting
};

string responseContent = string.Empty;

using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient(handler))
{
    System.Net.Http.HttpRequestMessage requestMessage = new System.Net.Http.HttpRequestMessage();

    requestMessage = new System.Net.Http.HttpRequestMessage
    {
        Content = myMultipartFormDataContent,
        Method = System.Net.Http.HttpMethod.Post,
        RequestUri = new Uri(strWebServiceURL)

    };

    using (System.Net.Http.HttpResponseMessage response = await client.SendAsync(requestMessage))
    {

        responseContent = await response.Content.ReadAsStringAsync();

    }

    //No difference when using it this way as well
    using (System.Net.Http.HttpResponseMessage response = await client.PostAsync(strWebServiceURL, myMultipartFormDataContent))
    {

        responseContent = await response.Content.ReadAsStringAsync();

    } 

我尝试使用Windows.Web.Http,但我不知道如何让它在不提示的情况下将当前/默认凭据传递给服务器。

我还将 WebService URL 添加到 IE 本地 Intranet 区域,并将该区域设置为使用当前用户名和密码自动登录:

请帮忙!

【问题讨论】:

    标签: c# xaml uwp windows-authentication wcf-web-api


    【解决方案1】:

    使用 UWP 应用中新的 Windows.Web.Http 命名空间,如果您想使用 DefaultCredentials,您只需在清单中打开企业凭据,uwp 应用就会根据需要将它们发送出去。您无需在 HttpClient 上配置任何内容即可使其正常工作。详情请参考this thread

    由于您已经启用了企业凭据功能,您可以直接创建 HttpClient 而无需配置。但是为了避免提示用户名和密码,你可能需要禁用 UI,例如:

    var myFilter = new HttpBaseProtocolFilter();
    myFilter.AllowUI = false;
    Windows.Web.Http.HttpClient client = new Windows.Web.Http.HttpClient(myFilter);
    Windows.Web.Http.HttpResponseMessage result = await client.GetAsync(new Uri("http://localhost:5132/api/values"));
    

    【讨论】:

    • 我在尝试使用 POST 请求上传文件时再次遇到同样的错误。做一个 post 请求(发送一个 jpg 文件),编码和媒体类型应该是什么?或者还有什么需要设置的吗?
    猜你喜欢
    • 1970-01-01
    • 2010-11-01
    • 1970-01-01
    • 2018-05-12
    • 2014-12-13
    • 1970-01-01
    • 2020-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多