如何在WCF中使用X509证书签名消息 [英] How to sign a message using X509 certificate in WCF
问题描述
我正在尝试使用需要邮件签名进行身份验证的Web服务。我创建了Web服务请求对象并用数据填充它。
我还使用X.509证书创建了一个签名令牌。
最终目的是申请令牌到Web服务请求,以便为请求的BODY创建数字签名,并将该签名附加到请求对象。
下面是我用来生成令牌的代码: br />
I am trying to consume a web service that requires message signing for authentication. i have Created the Web service request object and populated it with data.
I have also Created a signature token using the X.509 certificate.
The final aim is to apply the token to web service request so that a digital signature is created for the BODY of the request and that signature should get appended to the request object.
below is the code i am using to generate token :
public SecurityManager(string serviceActor, bool isClient, string clientActor)
: base(serviceActor, isClient, clientActor)
{
}
public override void SecureMessage(SoapEnvelope envelope, Security security)
{
// Get an X.509 certificate for signing the SOAP message.
X509SecurityToken signatureToken = GetSecurityToken("subjectName");
if (signatureToken == null)
{
throw new SecurityFault("Message Requirements could not be satisfied.");
}
// Add the X.509 certificate to the header.
security.Tokens.Add(signatureToken);
// Specify that the SOAP message is signed using this X.509
// certifcate.
MessageSignature sig = new MessageSignature(signatureToken);
security.Elements.Add(sig);
// Get an X.509 certificate for encrypting the SOAP message.
X509SecurityToken encryptionToken = GetSecurityToken("subjectName");
if (encryptionToken == null)
{
throw new SecurityFault("Message Requirements could not be satisfied.");
}
// Specify that the SOAP message is encrypted using
// this X.509 certificate.
EncryptedData enc = new EncryptedData(encryptionToken);
security.Elements.Add(enc);
}
public X509SecurityToken GetSecurityToken(string subjectName)
{
X509SecurityToken objX509SecurityToken = null;
X509Store objX509Store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
objX509Store.Open(OpenFlags.ReadOnly);
try
{
X509Certificate2Collection objX509Certificate2Collection = objX509Store.Certificates.Find(X509FindType.FindBySubjectName, subjectName, true);
X509Certificate2 objX509Certificate2;
if (objX509Certificate2Collection.Count == 1)
{
objX509Certificate2 = objX509Certificate2Collection[0];
objX509SecurityToken = new X509SecurityToken(objX509Certificate2);
}
else
{
objX509SecurityToken = null;
}
}
catch (Exception ex)
{
objX509SecurityToken = null;
}
finally
{
if (objX509Store != null)
objX509Store.Close();
}
return objX509SecurityToken;
}
}
以下是我用来创建网络服务请求的代码:
below is the code i am using to create web service request:
ServiceClient objServiceClient = new ServiceClient();
objServiceClient.send();
我想知道如何将安全令牌应用于我的网络服务请求。
推荐答案
https://msdn.microsoft.com/en-us/library/vstudio/aa702621(v = vs.100).aspx [ ^ ]
https://go4answers.webhost4life.com/Example /signingencrypting-web-service-client-8172.aspx [ ^ ]
请参考以上链接
https://msdn.microsoft.com/en-us/library/vstudio/aa702621(v=vs.100).aspx[^]
https://go4answers.webhost4life.com/Example/signingencrypting-web-service-client-8172.aspx[^]
Please refer to the above links
这篇关于如何在WCF中使用X509证书签名消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!