Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.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# 在Microsoft.Rest.ClientService中使用windows身份验证<;T>;_C#_Rest - Fatal编程技术网

C# 在Microsoft.Rest.ClientService中使用windows身份验证<;T>;

C# 在Microsoft.Rest.ClientService中使用windows身份验证<;T>;,c#,rest,C#,Rest,在vs 2015中使用“右键单击>>添加>>REST Api客户端”时,我已经为我的REST Api创建了客户端类。其他人可以很方便地快速开始使用我的api。 但我们必须使用windows凭据 因此,生成类时,如下所示: public partial class MarvalWebApi : ServiceClient<MarvalWebApi>, IMarvalWebApi { private Uri _baseUri; /// <s

在vs 2015中使用“右键单击>>添加>>REST Api客户端”时,我已经为我的REST Api创建了客户端类。其他人可以很方便地快速开始使用我的api。 但我们必须使用windows凭据

因此,生成类时,如下所示:

public partial class MarvalWebApi : ServiceClient<MarvalWebApi>, IMarvalWebApi
    {
        private Uri _baseUri;

        /// <summary>
        /// The base URI of the service.
        /// </summary>
        public Uri BaseUri
        {
            get { return this._baseUri; }
            set { this._baseUri = value; }
        }

        private ServiceClientCredentials _credentials;

        /// <summary>
        /// Credentials for authenticating with the service.
        /// </summary>
        public ServiceClientCredentials Credentials
        {
            get { return this._credentials; }
            set { this._credentials = value; }
        }
 ...
public分部类MarvalWebApi:ServiceClient,IMarvalWebApi
{
私有Uri _baseUri;
/// 
///服务的基本URI。
/// 
公共Uri BaseUri
{
获取{返回此。_baseUri;}
设置{this.\u baseUri=value;}
}
私有服务客户端凭据\u凭据;
/// 
///用于使用服务进行身份验证的凭据。
/// 
公共服务客户端凭据
{
获取{返回此。_凭据;}
设置{this.\u credentials=value;}
}
...
但我找不到将此凭据设置为类似Windows凭据的任何内容的方法

我尝试在web请求处理程序中设置凭据:

        var client = new MarvalWebApi();
        var webRequest = client.HttpMessageHandlers.OfType<WebRequestHandler>().First();
        webRequest.UseDefaultCredentials = true;
        //client.HttpMessageHandlers
        var res = client.OpImport
            .PostAsync(new MyParams())
            .ContinueWith(t =>
            {
                var e = t.Exception;

            });
        Task.WaitAll(res);
var client=new MarvalWebApi();
var webRequest=client.HttpMessageHandlers.OfType().First();
webRequest.UseDefaultCredentials=true;
//client.HttpMessageHandlers
var res=client.OpImport
.PostAsync(新的MyParams())
.ContinueWith(t=>
{
var e=t.例外;
});
Task.WaitAll(res);
我在执行请求时遇到异常

发生了一个或多个错误。 将内容复制到流时出错。 请求已中止:请求已取消

---   Error while copying content to a stream.

   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at ConsoleApplication1.OpImport.<PostWithOperationResponseAsync>d__4.MoveNext() in C:\Git\ConsoleApplication1\ConsoleApplication1\MarvalWebApi\OpImport.cs:line 111
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at ConsoleApplication1.OpImportExtensions.<PostAsync>d__1.MoveNext() in C:\Git\ConsoleApplication1\ConsoleApplication1\MarvalWebApi\OpImportExtensions.cs:line 42


--- The request was aborted: The request was canceled.
   at System.Net.Http.HttpClientHandler.WebExceptionWrapperStream.BeginRead(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state)
   at System.Net.Http.StreamToStreamCopy.StartRead()
---将内容复制到流时出错。
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()中
在C:\Git\ConsoleApplication1\ConsoleApplication1\MarvalWebApi\OpImport.cs中的ConsoleApplication1.OpImport.d_u4.MoveNext()处:第111行
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()中
在C:\Git\ConsoleApplication1\ConsoleApplication1\MarvalWebApi\OpImportExtensions.cs中的ConsoleApplication1.OpImportExtensions.d_u1.MoveNext()处:第42行
---请求已中止:请求已取消。
位于System.Net.Http.HttpClientHandler.WebExceptionWrapperStream.BeginRead(字节[]缓冲区、Int32偏移量、Int32计数、异步回调、对象状态)
在System.Net.Http.StreamToStreamCopy.StartRead()上

您能帮我理解其中的任何一个吗?

在Visual Studio的
添加>>REST Api客户端
的封面下,是

如果已安装Node.js,则可以使用
npm
将此项目安装到计算机上,如下所示:

npm install -g autorest
然后通过命令行生成客户端

代码将与您在Visual Studio中生成的代码几乎相同。您可能需要使用
--add credentials
标志来控制生成的
ServiceClient
类中可用的构造函数。(我将此设置为false,但下面是示例)


然后,你可以写一些东西让你的客户与Widows auth合作。

Hi,你解决了这个难题吗?我不得不将它移植到RestSharp