我已经完成了大部分基于堆栈溢出的其他人的代码的工作。见下文。
我目前的问题是,我仍然获得授权失败,我可以清楚地看到原因。v2 API要求传递和秘密以及SOAP安全头。但是,我不知道如何在使用从Wsdl文件创建的服务时注入它。
解决了问题:
克服名称空间问题(使用消息格式化程序Consume WCF Royal Mail API in c# Console Application)。
解决了绑定配置问题,导致两个安全头。此外,如果在检索maxReceivedMessageSize绑定时不想获得异常,则必须设置label.Final:
<system.serviceModel> <bindings> <basicHttpBinding> <binding name="basicBindingTransport" maxReceivedMessageSize="2147483647"> <security mode="Transport"> </security> </binding> </basicHttpBinding> </bindings> <client> <endpoint address="https://api.royalmail.net/shipping/v2" binding="basicHttpBinding" bindingConfiguration="basicBindingTransport" contract="RoyalMailApiWsdl.shippingAPIPortType" name="shippingAPIPort" /> </client> </system.serviceModel>
解决了E0007授权失败。
解决了HTTP请求是未经授权的客户端身份验证方案‘匿名’(您必须使用上面的绑定与安全“传输”,并将凭据直接注入http post报头本身(请参阅下面的答案)。
还有很多其他的问题,我现在都记不起来了。我希望这个帖子能对其他人有所帮助。
发布于 2016-08-11 12:14:47
要在航运API v2中实现双重身份验证,可以使用以下代码(取自How to add custom Http Header for C# Web Service Client consuming Axis 1.4 Web service的思想)
shippingAPIPortTypeClient client = GetProxy();
<..>
using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
{
var httpRequestProperty = new HttpRequestMessageProperty();
httpRequestProperty.Headers.Add(@"X-IBM-Client-Id", _credentials.HttpSecurity.ClientId);
httpRequestProperty.Headers.Add(@"X-IBM-Client-Secret", _credentials.HttpSecurity.ClientSecret);
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
createShipmentResponse response = client.createShipment(GetSecurityHeaderType(), request);
return response;
}
private shippingAPIPortTypeClient GetProxy()
{
// binding comes from configuration file
var shippingClient = new shippingAPIPortTypeClient();
shippingClient.ClientCredentials.UserName.UserName = _credentials.SoapSecurity.Username;
shippingClient.ClientCredentials.UserName.Password = _credentials.SoapSecurity.Password;
shippingClient.ClientCredentials.UseIdentityConfiguration = true;
foreach (OperationDescription od in shippingClient.Endpoint.Contract.Operations)
{
od.Behaviors.Add(new RoyalMailIEndpointBehavior());
}
return shippingClient;
}
private SecurityHeaderType GetSecurityHeaderType()
{
SecurityHeaderType securityHeader = new SecurityHeaderType();
DateTime created = DateTime.Now;
string creationDate;
creationDate = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ");
string nonce = nonce = (new Random().Next(0, int.MaxValue)).ToString();
byte[] hashedPassword;
hashedPassword = GetSHA1(_credentials.SoapSecurity.Password);
string concatednatedDigestInput = string.Concat(nonce, creationDate, Encoding.Default.GetString(hashedPassword));
byte[] digest;
digest = GetSHA1(concatednatedDigestInput);
string passwordDigest;
passwordDigest = Convert.ToBase64String(digest);
string encodedNonce;
encodedNonce = Convert.ToBase64String(Encoding.Default.GetBytes(nonce));
XmlDocument doc = new XmlDocument();
using (XmlWriter writer = doc.CreateNavigator().AppendChild())
{
writer.WriteStartDocument();
writer.WriteStartElement("Security");
writer.WriteStartElement("UsernameToken", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
writer.WriteElementString("Username", _credentials.SoapSecurity.Username);
writer.WriteElementString("Password", passwordDigest);
writer.WriteElementString("Nonce", encodedNonce);
writer.WriteElementString("Created", creationDate);
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
}
doc.DocumentElement.RemoveAllAttributes();
System.Xml.XmlElement[] headers = doc.DocumentElement.ChildNodes.Cast<XmlElement>().ToArray<XmlElement>();
securityHeader.Any = headers;
return securityHeader;
}在完全工作的皇家邮政服务API API v2解决方案的基础上,可以在C#中获得以下内容:https://github.com/povilaspanavas/RoyalMailShippingApiV2
它有三个测试,一个用来创建本地(GB)托运,另一个是国际测试,第三个是检索label文件。
https://stackoverflow.com/questions/38896117
复制相似问题