class UserNamePassValidator : System.IdentityModel.Selectors.UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
if (userName == null || password == null)
{
throw new ArgumentNullException();
}
if (!(userName == "MyUser" && password == "MyPass"))
{
throw new FaultException("Incorrect Username or Password");
}
}
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="wsHttpBinding" allowCookies="true"
maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" />
<security mode="Message">
<message clientCredentialType="UserName"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<client />
<services>
<service behaviorConfiguration="MyWCF_Behavior" name="MyWCF_ReportService.MyWCF_ReportService">
<endpoint address="wsHttpBinding" binding="wsHttpBinding" bindingConfiguration=""
name="wsHttpBinding" bindingName="wsHttpBinding" contract="MyWCF_ReportService.IMyWCF_ReportService" />
<endpoint address="mex" binding="mexHttpBinding" bindingConfiguration=""
name="mex" bindingName="mex" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost/MyWCF_ReportService/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyWCF_Behavior">
<serviceMetadata httpGetEnabled="true" />
<serviceCredentials>
<serviceCertificate findValue="localhost" storeLocation="LocalMachine"
storeName="My" x509FindType="FindBySubjectName" />
<userNameAuthentication userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="MyWCF_ReportService.UserNamePassValidator, MyWCF_ReportService" />
</serviceCredentials>
<serviceDebug />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
private void button1_Click(object sender, EventArgs e)
{
Dictionary<string, string> d = new Dictionary<string, string>()
{
{"MemberID", "MX12345"}
};
// Method 1: Create the client using the configuration file
MyWCF_ReportServiceReference.MyWCF_ReportServiceClient c = new MyWCF_ReportServiceReference.MyWCF_ReportServiceClient("wsHttpBinding");
c.ClientCredentials.UserName.UserName = "ussr";
c.ClientCredentials.UserName.Password = "pswd";
c.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
try
{
Byte[] ReportArray = c.GenerateSSRSReport("/DataMgt/Sample1", d);
SaveReport(ReportArray);
}
catch (FaultException<MyFaultException> ee) { textBox2.Text = ee.Detail.Reason; }
catch (System.InvalidOperationException ie) { textBox2.Text = ie.Message; }
catch (TimeoutException te) { textBox2.Text = te.Message; }
catch (FaultException fEx) { textBox2.Text = fEx.Message; }
catch (CommunicationException ce) { textBox2.Text = ce.Message; }
finally
{
c.Close();
}
}
public static void SaveReport(byte[] reportBytes)
{
...... //code to save file on server
}
customUserNamePasswordValidatorType="MyWCF_ReportService.UserNamePassValidator, MyWCF_ReportService"
Below links should help you.
http://msdn.microsoft.com/en-us/library/aa702565.aspx
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="wsHttpBinding" allowCookies="true"
maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" />
<security mode="Message">
<message clientCredentialType="UserName"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<security mode="Message">
<transport clientCredentialType="Basic" proxyCredentialType="Basic"
realm="" />
<message clientCredentialType="UserName" negotiateServiceCredential="true"
algorithmSuite="Default" />
</security>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="MyWCF_ReportService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="wsHttpBindingConfig" maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647" allowCookies="true">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" />
<security mode="Message">
<transport clientCredentialType="Basic" proxyCredentialType="Basic"
realm="" />
<message clientCredentialType="UserName" negotiateServiceCredential="true"
algorithmSuite="Default" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client />
<services>
<service behaviorConfiguration="MyWCF_Behavior" name="MyWCF_ReportService.MyWCF_ReportService">
<endpoint address="wsHttpBinding" binding="wsHttpBinding" bindingConfiguration="wsHttpBindingConfig"
name="wsHttpBinding" bindingName="wsHttpBinding" contract="MyWCF_ReportService.IMyWCF_ReportService" />
<endpoint address="mex" binding="mexHttpBinding" bindingConfiguration=""
name="mex" bindingName="mex" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost/MyWCF_ReportService/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyWCF_Behavior">
<serviceMetadata httpGetEnabled="true" />
<serviceCredentials>
<serviceCertificate findValue="localhost" storeLocation="LocalMachine"
storeName="My" x509FindType="FindBySubjectName" />
<userNameAuthentication userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="MyWCF_ReportService.CustomUserNamePassValidator, MyWCF_ReportService" />
</serviceCredentials>
<serviceDebug />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<applicationSettings>
<MyWCF_ReportService.Properties.Settings>
<setting name="MyWCF_ReportService_RE2005_ReportExecutionService"
serializeAs="String">
<value>http://myserver/ReportServer/ReportExecution2005.asmx</value>
</setting>
<setting name="MyWCF_ReportService_RS2005_ReportingService2005"
serializeAs="String">
<value>http://myserver/ReportServer/ReportService2005.asmx</value>
</setting>
</MyWCF_ReportService.Properties.Settings>
</applicationSettings>
</configuration>
[DataContract]
public class MyFaultException
{
private string _reason;
[DataMember]
public string Reason
{
get { return _reason; }
set { _reason = value; }
}
}
class CustomUserNamePassValidator : System.IdentityModel.Selectors.UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
if (userName == null || password == null)
{
MyFaultException theFault = new MyFaultException();
theFault.Reason = "Security Error Missing Credentials: ";
throw new FaultException<MyFaultException>(theFault);
}
if (!(userName == "MyUser" && password == "MyPass"))
{
MyFaultException theFault = new MyFaultException();
theFault.Reason = "Security Error Wrong Credentials: ";
throw new FaultException<MyFaultException>(theFault);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
Byte[] ReportArray = null;
MyWCF_DesktopClient.MyWCF_ReportServiceReference.MyWCF_ReportServiceClient c = new MyWCF_DesktopClient.MyWCF_ReportServiceReference.MyWCF_ReportServiceClient("wsHttpBinding");
c.ClientCredentials.UserName.UserName = User;
c.ClientCredentials.UserName.Password = Pass;
c.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
c.Open();
try
{
ReportArray = c.CreateReport("WX0143441");
}
catch (FaultException<MyFaultException> ee) { textBox2.Text = ee.Detail.Reason; }
catch (FaultException fEx) { textBox2.Text = fEx.Message; }
finally
{
c.Close();
}
if (ReportArray != null)
{
try
{
SaveReport(ReportArray);
}
catch (Exception)
{
throw new Exception("File save exception");
}
}
}
class CustomUserNamePassValidator : System.IdentityModel.Selectors.UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
if (userName == null || password == null)
{
throw new SecurityTokenException("security Error: No Credentials Passed");
}
if (!(userName == "MyUser" && password == "MyPass"))
{
throw new SecurityTokenException("security Error: Wrong Credentials Passed");
}
}
}
private void button2_Click(object sender, EventArgs e)
{
Byte[] ReportArray = null;
credentials MyCredentials = new credentials();
// Create the client using the configuration file
MyWCF_DesktopClient.MyWCF_ReportServiceReference.MyWCF_ReportServiceClient c = new MyWCF_DesktopClient.MyWCF_ReportServiceReference.MyWCF_ReportServiceClient("wsHttpBinding");
//MyCredentials = GetWebServiceCredentials();
//test with bad credentials
MyCredentials.UserName = "uuuu";
MyCredentials.Passwrd = "pppp";
///////////////////////////////
c.ClientCredentials.UserName.UserName = MyCredentials.UserName;
c.ClientCredentials.UserName.Password = MyCredentials.Passwrd;
c.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
try
{
c.Open();
ReportArray = c.CreateReport("WX0143441");
}
catch (FaultException<MyFaultException> ee) { textBox2.Text = ee.Detail.Reason; }
catch (FaultException fEx) { textBox2.Text = fEx.Message; }
catch (Exception excp) { textBox2.Text = excp.Message; }
finally
{
c.Close(); //***********exception thrown here************
}
if (ReportArray != null)
{
try
{
SaveReport(ReportArray);
}
catch (Exception)
{
throw new Exception("File save exception");
}
}
}
Thanks.