This question is an exact duplicate of:
I want to implement message signing using WCF. I know how to implement message signing in WSE, but i am facing issues while try to implement the same in WCF. I have already installed X509 certificate on my local machine, I have also successfully tested my application in SOAP UI tool. Below is the code i am using to achieve the same:
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;
}
// 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);
Aucun commentaire:
Enregistrer un commentaire