2

When using "Right-click >> Add >> REST Api client" in vs 2015 I've created client classes for my rest api. It's convenient for other guys to quickly get going with my api. But we have to use windows credentials.

So when the class is generated looks like that:

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; }
        }
 ...

But I can't find a way to set this Credentials to anything like Windows Credentials.

I tried to set the credentials in web request handler:

        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);

I get exception when executing the request.

One or more errors occurred. Error while copying content to a stream. The request was aborted: The request was canceled.

---   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()

Can you help me understand any of these, please?

jjaskulowski
  • 2,524
  • 3
  • 26
  • 36

1 Answers1

0

Under the covers of Visual Studio's Add >> REST Api Client, is autorest

If you have Node.js installed, you can use npm to install this project to your machine like this:

npm install -g autorest

and then generate clients via command line.

The code will be near identical to what you've generated in Visual Studio. You may need to use the --add-credentials flag to control what constructors are available in your generated ServiceClient<T> class. (I set this to false, but here's the Command Line documentation)

Then, you can write code like this to get your client going with Widows auth.

bkwdesign
  • 1,953
  • 2
  • 28
  • 50