Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Mac-如何将当前凭据传递给REST端点_C#_Xamarin_Restful Authentication_Xamarin.mac - Fatal编程技术网

C# Mac-如何将当前凭据传递给REST端点

C# Mac-如何将当前凭据传递给REST端点,c#,xamarin,restful-authentication,xamarin.mac,C#,Xamarin,Restful Authentication,Xamarin.mac,无法使用System.Net API向REST终结点验证当前用户。下面的示例返回401个未经授权的 using (HttpClient client = new HttpClient()) { using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://rest/api/endpoint") { using (httpResponseMessage respo

无法使用System.Net API向REST终结点验证当前用户。下面的示例返回401个未经授权的

using (HttpClient client = new HttpClient())
{
    using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://rest/api/endpoint")
    {
        using (httpResponseMessage response = await client.SendAsync(request))
        {
            if (response.IsSuccessStatusCode)
                //do something
        }
    }
}
如果我使用NSUrlConnection,我就能够进行身份验证,这不是问题。因此,NSUrlConnection必须以某种方式传递凭据。下面是该代码的一个片段:

var request = new NSMutableUrlRequest(new NSUrl("http://rest/api/endpoint"), NSUrlRequestCachePolicy.ReloadIgnoringCacheData, 0);
request["Accept"] = "application/json";
NSUrlConnection.SendAsynchronousRequest(request, NSOperationQueue.MainQueue, delegate(NSUrlResponse, response, NSData data, NSError error)
{
    // successfully authenticated and do something with response
});
我想将我的服务代码包装在PCL中,以便与其他平台共享。因此,我希望所有这些都能在System.NETAPI中运行。这可能吗

更新:

我尝试过使用HttpClientHandler,使用默认凭据以及CredentialCache.DefaultNetworkCredentials。让它工作的唯一方法是硬编码凭证,这是我不想要的。系统.Net堆栈似乎没有显示来自操作系统的凭据。

我假设它使用默认身份验证。您可以使用带有HttpClientHandler的HttpClient执行此操作,例如:

using (var handler = new HttpClientHandler {
    UseDefaultCredentials = true
})
using (var client = new HttpClient(handler))
{
    var result = await client.SendAsync(...);
}

我假设您需要添加一个用于授权的头,这取决于端点。在您最喜欢的搜索引擎中键入
c#httpclient authenticate
应该已经回答了这个问题。这只有在我将硬编码凭据与标题一起发送时才有效。但是,我想使用当前登录mac.Hmm的用户的当前凭据,这可能是HttpClient的一个限制。