Below is the c# code and config files.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using ConsoleApplication1.ServiceReference1;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
//Create a RightsManagementServiceClient object
RightsManagementServiceClient rmClient = new RightsManagementServiceClient();
rmClient.Endpoint.Address = new System.ServiceModel.EndpointAddress("https://IPAddressofSite/soap/services/RightsManagementService?blob=mtom");
//Enable BASIC HTTP authentication
BasicHttpBinding b = (BasicHttpBinding)rmClient.Endpoint.Binding;
b.MessageEncoding = WSMessageEncoding.Mtom;
rmClient.ClientCredentials.UserName.UserName = "********";
rmClient.ClientCredentials.UserName.Password = "*********";
b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
//b.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
b.Security.Mode = BasicHttpSecurityMode.Transport;
//Create a BLOB that represents the PDF document
//to which a policy is applied
BLOB inDoc = new BLOB();
//Reference the PDF document to which a policy is applied
string inputFileName = @"P:\PDF\TextToPDf.pdf";
FileStream fs = new FileStream(inputFileName, FileMode.Open);
//Get the length of the file stream
int len = (int)fs.Length;
byte[] ByteArray = new byte[len];
//Populate the byte array with the contents of the FileStream object
fs.Read(ByteArray, 0, len);
inDoc.MTOM = ByteArray;
//Prepare output parameters
string PolicyID;
string DocumentID;
string MimeType;
SetCertificatePolicy();
//Apply a policy to a PDF document named Loan.pdf
BLOB outDoc = rmClient.protectDocument(
inDoc,
"TextToPDf.pdf",
"Test Policy Set",
"Test Policy 1",
null,
null,
ConsoleApplication1.ServiceReference1.RMLocale.en,
out PolicyID,
out DocumentID,
out MimeType);
//Populate a byte array with the contents of the BLOB
byte[] outByteArray = outDoc.MTOM;
//Create a new file containing the policy-protected PDF document
string FILE_NAME = @"P:\PDF\PolicyProtectedLoanDoc.pdf";
FileStream fs2 = new FileStream(FILE_NAME, FileMode.OpenOrCreate);
BinaryWriter w = new BinaryWriter(fs2);
w.Write(outByteArray);
w.Close();
fs2.Close();
}
catch (Exception ee)
{
Console.WriteLine(ee.Message);
Console.ReadKey();
}
}
/// <summary>
/// Sets the cert policy.
/// </summary>
public static void SetCertificatePolicy()
{
ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate;
}
/// <summary>
/// Remotes the certificate validate.
/// </summary>
private static bool RemoteCertificateValidate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
{
// trust any certificate!!!
System.Console.WriteLine("Warning, trust any certificate");
return true;
}
}
}
web.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="RightsManagementServiceSoapBinding" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="Transport">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="AXIS" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
<binding name="RightsManagementServiceSoapBinding1" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://IPAddressofSite/soap/services/RightsManagementService"
binding="basicHttpBinding" bindingConfiguration="RightsManagementServiceSoapBinding"
contract="ServiceReference1.RightsManagementService" name="RightsManagementService" />
</client>
</system.serviceModel>
</configuration>