【问题标题】:How to pass header in Azure endpoint..?如何在 Azure 端点中传递标头..?
【发布时间】:2016-11-30 11:27:35
【问题描述】:

我正在使用 Azure API ,URL 低于错误请帮助解决这个问题。请分享代码片段,如何更改web.config 和端点。

HTTP 请求未经客户端身份验证方案授权 '匿名的'。从服务器收到的身份验证标头是 'AzureApiManagementKey realm="https://azure.azure-api.net/MethodName",name="Ocp-Apim-Subscription-Key",type="header"'。

【问题讨论】:

  • 您能否添加更多详细信息,说明您正在尝试做什么以及客户端应用程序等等。
  • 我正在使用 API Management 提供的 WSDL,并在调用该服务中的方法时遇到此问题。
  • 你用 API Management 提供的 WSDL 做什么? APIM 中的 API 是如何创建的?通过 WSDL?您收到的错误来自哪里?

标签: asp.net azure azure-api-apps azure-api-management azure-hub


【解决方案1】:

我知道这仍然是一个非常古老的问题,我的回答将帮助面临同样问题的人。

解决方案是创建自定义端点行为,在其中将自定义消息处理程序添加到绑定参数。

在自定义消息处理程序中,请添加您的请求标头。在此之后,使用安全模式为“Transport”、MessageEncoding 为“Text”的任何绑定技术(如 basichttpsbinding 或 NetHttpsBinding)来创建soap客户端对象。将自定义端点行为添加到 soap 客户端。

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

使用服务的控制台应用程序。

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

【讨论】:

  • 上述解决方案是假设APIM注册是在没有任何其他认证机制的情况下完成的。如果您的 SOAP 直通服务需要任何身份验证,您可以使用此解决方案添加它。
【解决方案2】:

这是在抱怨您的订阅密钥错误。如果您检查响应正文,它将为您提供有关真正问题所在的可读消息。仔细检查您是否为 Azure API 访问输入了正确的订阅密钥。

您可以从个人资料菜单下的开发人员门户中获取订阅密钥。您可以在“从开发者门户调用操作”部分下查看本文中使用的订阅密钥示例:https://docs.microsoft.com/en-us/azure/api-management/api-management-get-started

此外,“HTTP 请求未经客户端身份验证方案‘匿名’授权。”消息的一部分是红鲱鱼和响应如何工作的一个单独问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-15
    • 2015-05-20
    • 2018-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多