asp.net - 如何在Azure端点中传递 header ..?

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

我正在使用 Azure API ,URL 出现以下错误,请帮助解决此问题。请分享codesnip,如何更改web.config和端点。

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"'.

最佳答案

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

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

在自定义消息处理程序中,请添加您的请求 header 。之后,使用任何绑定(bind)技术(如 basichttpsbinding 或 NetHttpsBinding),安全模式为“Transport”,MessageEncoding 为“Text”来创建肥皂客户端对象。将自定义端点行为添加到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;
    }
}

关于asp.net - 如何在Azure端点中传递 header ..?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40887199/

相关文章:

c# - HttpContext.Current.Request.ApplicationPath 在生产环境中为空

azure - 如何在单个 Azure VM 上安装 2 个 SSL 证书

wcf - 托管 wcf 服务的 Azure API 应用程序服务

jquery - CORS 预检问题

azure - DocumentDB : Is it possible to remove auto generated tags, _Etag、_ts 等?

c# - 如何使用 jQuery 复制带有下拉列表的表行

html - 在屏幕的右半部分放置 6 个可滚动表格,一个放在另一个下面

javascript - 单击同一个链接时如何停止多个链接

azure - 无法创建从 Azure Blob 存储容器到 Azure Sql 数据库超大规模的外部数据源

asp.net - .NET MVC 应用程序 - Azure Active Directory - 重定向到 LocalHost