我正在使用 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/