WilliamNadeau | Posts: 6

Issues with Authentication on Sandbox

0 votes
Hi, I'm trying to set up a demonstration project using the sandbox environment but I'm having troubles authenticating with the API. I initially used the .NET SDK to access the sandbox but when I hit troubles I decided to try and distill the problem down. What I'm observing is that my first attempt to connect to the sandbox API always yields a 401, even when I am explicitly supplying the API Key in the Authorization header. As long as I use the same HttpClient instance, thereafter I can then get through without problems. I've replicated this behavior in Postman, so I don't believe it is .NET specific. Here's the code:
        private static HttpClient client = CreateClient();

        static async Task Main(string[] args)
        {
            await Go(client);
            await Go(client);
            await Go(client);
        }

        private static HttpClient CreateClient()
        {
            var handler = new HttpClientHandler()
            {
                PreAuthenticate = true,
            };

            var httpClient = new HttpClient(handler)
            {
            };
            
            httpClient.DefaultRequestHeaders.Add("Authorization", "Basic {api key goes here}");

            httpClient.BaseAddress = new Uri("https://sandbox.esignlive.com/api/");
            return httpClient;
        }

        private static async Task Go(HttpClient httpClient)
        {
            try
            {
                var package = await httpClient.GetAsync("packages");
                Console.WriteLine($"Got packages: {await package.Content.ReadAsStringAsync()}");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
Outcome:
Got packages: {"messageKey":"error.unauthorised.noSession","message":"Failed to retrieve Session","code":401,"name":"Unauthorized"}
Got packages: {"count":0,"results":[]}
Got packages: {"results":[],"count":0}
I am behind a proxy that I think might be part of the issue, but I am not sure. I had to configure the default .NET proxy as such:

    
      
    
  
Any ideas on what might be gumming up the works? Thanks.

mwilliams | Posts: 957

Reply to: Issues with Authentication on Sandbox

0 votes
Hello, And you're certain that the API Key is correct and the OneSpanSign instance you're pointing to is the one that you can log into via your browser?

WilliamNadeau | Posts: 6

Reply to: Issues with Authentication on Sandbox

0 votes
Yes. I can receive successful responses, which means that the API key must be valid and the URI correct. It's just that the first call always fails. This would be a problem that I could work around by keeping HttpClient alive for the whole application lifecycle and automatically retrying 401's, but I was hoping to be able to use the SDK, and the way SDK works it causes it to fail every request. I would rather not have to roll my own REST client nor modify the SDK code to get it working (if I can avoid those options).

mwilliams | Posts: 957

Reply to: Issues with Authentication on Sandbox

0 votes
What version of the SDK are you using and are you having this issue only in Sandbox or are you seeing it in Production, too? It shouldn't be related to the proxy if you're able to connect on the 2nd try, I wouldn't think. Do you have access directly to the proxy server to see if this same issue happens without having to proxy? Or a computer/server that doesn't require the proxy setup?

WilliamNadeau | Posts: 6

Reply to: Issues with Authentication on Sandbox

0 votes
I'm using the .NET SDK version 11.13. I have only connected to sandbox and I'm only working with a development account so far. I'm on a corporate network so I don't have access to the proxy. I haven't attempted to use it outside of the corporate network yet, I'll give that a try tonight and see if can connect clean.

mwilliams | Posts: 957

Reply to: Issues with Authentication on Sandbox

0 votes
Checking back in on this. Did you have success outside the corporate network?

WilliamNadeau | Posts: 6

Reply to: Issues with Authentication on Sandbox

0 votes
I did, and I was able to get through with no issues.

PatrickMcDonald | Posts: 2

Reply to: Issues with Authentication on Sandbox

0 votes
I found that calling the following code before using the SDK resolved the proxy issue for me WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultNetworkCredentials; This won't work for using the HttpClient, it only works for the SDK, which seems to use System.Net.WebRequest internally Edit: Adding the following to the App.config has also worked for me

mwilliams | Posts: 957

Reply to: Issues with Authentication on Sandbox

0 votes
Thanks for the post, Patrick! William, Sorry for not getting back to you, yesterday. I have been in meetings all day the last couple days. I meant to ask if you were setting up the proxy configuration on your client. See the following guide: Proxy Configuration Patrick, I guess I could ask the same question to you. :) Please let me know!

PatrickMcDonald | Posts: 2

Reply to: Issues with Authentication on Sandbox

1 votes
Sorry for hijacking this thread... My own personal problem is that my company uses NTLM authentication to the proxy, and I don't want to put my LAN username and password in the program/configs By default the WebRequest uses the system proxy, and setting the DefaultWebProxy.Credentials to CredentialCache.DefaultNetworkCredentials results in a successful authentication at the proxy for me Many thanks, Patrick

mwilliams | Posts: 957

Reply to: Issues with Authentication on Sandbox

0 votes
Not a problem Patrick. I think this is great information for any others that might have your same situation.

WilliamNadeau | Posts: 6

Reply to: Issues with Authentication on Sandbox

0 votes
I did try both of the suggestions, configuring the client with the proxy information as well as setting the default proxy credentials, both seem to fail. At this point I believe that the issue is on our side, probably with configuration of the proxy. I'll continue to investigate on my own and post back if I find evidence to the contrary. Thanks for your assistance!

mwilliams | Posts: 957

Reply to: Issues with Authentication on Sandbox

0 votes
Thanks for the update, William! Please let us know what you find.

mwilliams | Posts: 957

Reply to: Issues with Authentication on Sandbox

0 votes
Hey William, I just wanted to check back in and see if you discovered the issue or if there's anything else I/we can do to help. Thanks,

WilliamNadeau | Posts: 6

Reply to: Issues with Authentication on Sandbox

0 votes
Sorry for the delay. We discovered that the issue is on our side, though we haven't fixed it yet. I appreciate the help and the follow-up, though. Thank you.

Hello! Looks like you're enjoying the discussion, but haven't signed up for an account.

When you create an account, we remember exactly what you've read, so you always come right back where you left off