我正在從一家銀行實施一個API,他們需要提供一個安全令牌。在每一個肥皂消息的頭部中有一些,其看起來如下:如何在WCF soap響應中實現安全令牌?
<soapenv:Header>
<tpw:BinarySecurityToken ValueType="MAC" Id="DesMacToken" EncodingType="Base64" Value="**xvz**"/>
</soapenv:Header>
根據它們的文檔我需要生成每個消息的主體8字節MAC值。 MAC由CBC-MAC算法和DES生成,作爲分組密碼。每條消息的soapenv:Body標籤的內容被用作MAC計算的數據。
所以我的問題是如何讓WCF做到這一點?我將下面的代碼放在一起來創建MAC值,但我不確定如何將這個代碼放入每條消息的標題中。
private string GenerateMAC(string SoapXML)
{
ASCIIEncoding encoding = new ASCIIEncoding();
//Convert from Hex to Bin
byte[] Key = StringToByteArray(HexKey);
//Convert String to Bytes
byte[] XML = encoding.GetBytes(SoapXML);
//Perform the Mac goodies
MACTripleDES DesMac = new MACTripleDES(Key);
byte[] Mac = DesMac.ComputeHash(XML);
//Base64 the Mac
string Base64Mac = Convert.ToBase64String(Mac);
return Base64Mac;
}
public static byte[] StringToByteArray(string Hex)
{
if (Hex.Length % 2 != 0)
{
throw new ArgumentException();
}
byte[] HexAsBin = new byte[Hex.Length/2];
for (int index = 0; index < HexAsBin.Length; index++)
{
string bytevalue = Hex.Substring(index * 2, 2);
HexAsBin[index] = Convert.ToByte(bytevalue, 16);
}
return HexAsBin;
}
任何幫助將不勝感激。
更多信息: 該銀行提供了一個我曾經用作服務參考的WSDL。即發送一個響應的一個例子:
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.MessageContractAttribute(WrapperName="LogonRequest", WrapperNamespace="http://webservice.com", IsWrapped=true)]
public partial class LogonRequest {
[System.ServiceModel.MessageHeaderAttribute(Namespace="http://webservice.com")]
public DataAccess.BankService.BinarySecurityToken BinarySecurityToken;
的的BinarySecurityToken(即去的報頭)如下所示:
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.233")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://webservice.com")]
public partial class BinarySecurityToken : object, System.ComponentModel.INotifyPropertyChanged {
private string valueTypeField;
private string idField;
private string encodingTypeField;
private string valueField;
public BinarySecurityToken() {
this.valueTypeField = "MAC";
this.idField = "DesMacToken";
this.encodingTypeField = "Base64";
}
謝謝!通過一些調整,它完美的工作! – Dylan 2012-04-20 11:50:51