c# - 将当前身份验证凭据从 UWP 应用程序传递到 Web 服务,以使用这些凭据访问服务器上的资源

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

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

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

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

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

This IRandomAccessStream does not support the GetInputStreamAt method because it requires cloning and this stream does not support cloning.

我读到这是此链接上的错误: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();

    }

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

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 区域,并将该区域设置为使用当前用户名和密码自动登录:

请帮忙!

IE Settings

最佳答案

在 UWP 应用程序中使用新的 Windows.Web.Http 命名空间,如果您想使用 DefaultCredentials,您只需在 list 和 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"));

关于c# - 将当前身份验证凭据从 UWP 应用程序传递到 Web 服务,以使用这些凭据访问服务器上的资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49417081/

相关文章:

c# - 将 SQLite 数据填充到 UWP 应用程序中

c# - UWP 通知 Toast 已激活、更新和过期

c# - 如何在自动滚动设置为 false 的情况下使用可滚动控件

c# - WPF DataGrid 到 csv,只导出网格中的可见值

c# - 反序列化xlink :href

wpf - 如何将 XAML 绑定(bind)延迟到加载后

wpf - 我可以从哪里下载 Microsoft 的标准 WPF 主题?

c# - 在 C# UWP Windows 10 应用程序中读取和写入 Excel 工作表

c# - 在 headless (headless)模式下独立运行 Unity,同时捕获屏幕截图

c# - 如何在记录类型中添加默认值? - F#