C# 将当前身份验证凭据从UWP应用程序传递到web服务,以使用这些凭据访问服务器上的资源
我的Windows 10 UWP应用程序正在调用我创建的WebAPI web服务。调用web服务时,我需要在客户端传递当前凭据,以便它可以使用这些凭据访问其他资源 我还需要在不提示用户输入凭据的情况下执行此操作,以便体验无缝 我可以通过使用C# 将当前身份验证凭据从UWP应用程序传递到web服务,以使用这些凭据访问服务器上的资源,c#,xaml,uwp,windows-authentication,wcf-web-api,C#,Xaml,Uwp,Windows Authentication,Wcf Web Api,我的Windows 10 UWP应用程序正在调用我创建的WebAPI web服务。调用web服务时,我需要在客户端传递当前凭据,以便它可以使用这些凭据访问其他资源 我还需要在不提示用户输入凭据的情况下执行此操作,以便体验无缝 我可以通过使用System.Net.Http来实现这一点,并成功地将当前凭据传递给服务器以用于访问资源。这将发送请求并返回响应,而无需任何提示。我已在UWP应用程序上启用了企业身份验证和专用网络功能,以实现此功能 问题:这适用于GET请求,但不适用于对同一服务器的POST请
System.Net.Http
来实现这一点,并成功地将当前凭据传递给服务器以用于访问资源。这将发送请求并返回响应,而无需任何提示。我已在UWP应用程序上启用了企业身份验证
和专用网络
功能,以实现此功能
问题:这适用于GET请求,但不适用于对同一服务器的POST请求。POST请求导致以下错误:
此IRandomaccesstream不支持GetInputStreamAt方法
因为它需要克隆,而此流不支持克隆
我了解到这是此链接上的一个错误:。针对此错误,在多个位置建议的解决方法是使用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请求的代码,该请求会导致irandomaccesstream
错误:
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区域,并将该区域设置为使用当前用户名和密码自动登录:
请帮忙强>
在UWP应用程序中使用新的
Windows.Web.Http
命名空间,如果要使用DefaultCredentials
,只需在清单中打开企业凭据,UWP应用程序将根据需要发送它们。无需在HttpClient
上配置任何东西即可使其正常工作。详情请参考
由于您已经启用了企业凭据功能,因此可以只创建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文件),编码和媒体类型应该是什么?或者我还有什么需要安排的吗?