SOAP 调用中的授权 Header
Authorization Header in SOAP call
我将 WSDL 导入到我的 C# .NET 项目中。之后我必须生成一个访问令牌,现在我必须在调用 SOAP 服务时通过授权 header 使用此令牌。有什么简单的方法吗?
MemberAccountPortClient clientTransaction = new MemberAccountPortClient ("SERVICE");
SearchTransactionResponseType res = clientTransaction.searchTransaction (OBJECT_1, OBJECT_2);
在这种情况下如何添加授权header?
您可以创建一个 IClientMessageInspector
/ IEndpointBehavior
来设置这个值,如下所示:(是的,这段代码很冗长,但这就是 WCF 的工作方式;)
using System;
using System.Net;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
public class AuthorizationHeaderMessageInspector : IClientMessageInspector, IEndpointBehavior
{
object IClientMessageInspector.BeforeSendRequest(ref Message request, IClientChannel channel)
{
HttpRequestMessageProperty prop;
Object obj;
if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out obj))
{
prop = (HttpRequestMessageProperty)obj; // throws a cast exception if invalid type
}
else
{
prop = new HttpRequestMessageProperty();
request.Properties.Add(HttpRequestMessageProperty.Name, prop);
}
prop.Headers[HttpRequestHeader.Authorization] = "your authorization value here";
return null;
}
void IClientMessageInspector.AfterReceiveReply(ref Message reply, object correlationState)
{
}
void IEndpointBehavior.AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
void IEndpointBehavior.ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.MessageInspectors.Add(this);
}
void IEndpointBehavior.ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
}
void IEndpointBehavior.Validate(ServiceEndpoint endpoint)
{
}
}
然后,当您创建客户端时,按如下方式添加消息检查器:
MemberAccountPortClient clientTransaction = new MemberAccountPortClient ("SERVICE");
clientTransaction.Endpoint.Behaviors.Add(new AuthorizationHeaderMessageInspector());
SearchTransactionResponseType res = clientTransaction.searchTransaction (OBJECT_1, OBJECT_2);
我相信 WCF 也有一种使用配置来应用 IEndpointBehavior
的方法,但我通常会直接为这些类型的事情编写代码。
我将 WSDL 导入到我的 C# .NET 项目中。之后我必须生成一个访问令牌,现在我必须在调用 SOAP 服务时通过授权 header 使用此令牌。有什么简单的方法吗?
MemberAccountPortClient clientTransaction = new MemberAccountPortClient ("SERVICE");
SearchTransactionResponseType res = clientTransaction.searchTransaction (OBJECT_1, OBJECT_2);
在这种情况下如何添加授权header?
您可以创建一个 IClientMessageInspector
/ IEndpointBehavior
来设置这个值,如下所示:(是的,这段代码很冗长,但这就是 WCF 的工作方式;)
using System;
using System.Net;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
public class AuthorizationHeaderMessageInspector : IClientMessageInspector, IEndpointBehavior
{
object IClientMessageInspector.BeforeSendRequest(ref Message request, IClientChannel channel)
{
HttpRequestMessageProperty prop;
Object obj;
if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out obj))
{
prop = (HttpRequestMessageProperty)obj; // throws a cast exception if invalid type
}
else
{
prop = new HttpRequestMessageProperty();
request.Properties.Add(HttpRequestMessageProperty.Name, prop);
}
prop.Headers[HttpRequestHeader.Authorization] = "your authorization value here";
return null;
}
void IClientMessageInspector.AfterReceiveReply(ref Message reply, object correlationState)
{
}
void IEndpointBehavior.AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
void IEndpointBehavior.ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.MessageInspectors.Add(this);
}
void IEndpointBehavior.ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
}
void IEndpointBehavior.Validate(ServiceEndpoint endpoint)
{
}
}
然后,当您创建客户端时,按如下方式添加消息检查器:
MemberAccountPortClient clientTransaction = new MemberAccountPortClient ("SERVICE");
clientTransaction.Endpoint.Behaviors.Add(new AuthorizationHeaderMessageInspector());
SearchTransactionResponseType res = clientTransaction.searchTransaction (OBJECT_1, OBJECT_2);
我相信 WCF 也有一种使用配置来应用 IEndpointBehavior
的方法,但我通常会直接为这些类型的事情编写代码。