Jitendra Shewale
Jitendra Shewale

Reputation: 21

How to pass header in Azure endpoint..?

I am using Azure API , URL getting below error please help on this issue. please share codesnip, how to change in web.config and endpoints.

The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'AzureApiManagementKey realm="https:/azure.azure-api.net/MethodName",name="Ocp-Apim-Subscription-Key",type="header"'.

Upvotes: 1

Views: 1303

Answers (2)

Arul
Arul

Reputation: 121

I know this is a very old question still, my answer would help someone faces the same issue.

The solution is to create a custom endpoint behavior where you add a custom message handler to the binding parameters.

In the custom message handler, please add your request headers. After this, use any of the binding technique (like basichttpsbinding or NetHttpsBinding) with security mode as "Transport" and MessageEncoding as "Text" for creating soap client object. Add custom endpoint behavior to the soap client.

public class CustomEndpointBehavior : IEndpointBehavior
{
    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {
        bindingParameters.Add(new Func<HttpClientHandler, HttpMessageHandler>(x =>
        {
            return new CustomMessageHandler(x);
        }));
    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) { }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { }

    public void Validate(ServiceEndpoint endpoint) { }
}

public class CustomMessageHandler : DelegatingHandler
{
    public CustomMessageHandler(HttpClientHandler handler)
    {
        InnerHandler = handler;
    }

    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
    {
        request.Headers.Add("xxxx", "abcde");
        return base.SendAsync(request, cancellationToken);
    }
}

The console app to consume the service.

static async Task Main(string[] args)
{
        var client = GetSOAPClient();

        try
        {
            var result = await client.MyOperation().ConfigureAwait(false);
            if(result.Body != null && result.Body.status == "Success")
            {
                Console.WriteLine(result.Body.myValue);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex?.Message);
        }

        Console.ReadKey();
    }

    static MyServiceClient GetSOAPClient()
    {
        NetHttpsBinding binding = new NetHttpsBinding();
        binding.Security.Mode = BasicHttpsSecurityMode.Transport;
        binding.MessageEncoding = NetHttpMessageEncoding.Text;
        EndpointAddress ea = new EndpointAddress(new Uri("https://myazureurl"));

        var client = new MyServiceClient(binding, ea);
        client.Endpoint.EndpointBehaviors.Add(new CustomEndpointBehavior());

        return client;
    }
}

Upvotes: 1

Rudy Scoggins
Rudy Scoggins

Reputation: 451

This is complaining that your Subscription key is wrong. If you check the response body, it will give you a readable message of what the real problem is. Double check you are entering the correct subscription key for your Azure API access.

You get your subscription key from the Developer Portal under your profile menu. You can see an example of the subscription key being used in this article under the section "Call an operation from the developer portal": https://learn.microsoft.com/en-us/azure/api-management/api-management-get-started

Also, the 'The HTTP request is unauthorized with client authentication scheme 'Anonymous'.' part of the message is a red herring and a separate problem with how responses work.

Upvotes: 0

Related Questions