Wednesday, August 22, 2012

Ads Appear in Lower Right of Browser


This problem may present itself as random redirects or as a big square ad appearing in the lower right corner of the web browser which when clicked, may lead to malicious sites.

  
This is usually an indication that the hosts file of the computer have been hijacked.
I've been able to fix this problem by resetting my hosts file manually or by using RogueKiller which is available via the following link:
http://www.sur-la-toile.com/RogueKiller/RogueKiller.exe

Friday, August 10, 2012

WCF: Username/Password Authentication using BasicHttpBinding


I was required to do this type of Authentication in one of my projects. I am documenting the steps that I did for future reference.

Create an X509 certificate

The username/password authentication requires the communication between the client and server to be encrypted.

A test certificate can be created using the Certificate Creation Tool (Makecert.exe) by Microsoft.
http://msdn.microsoft.com/en-us/library/bfsktky3(v=vs.80).aspx
  1. Open the Visual Studio command prompt
  2. Type the following command
Certificate Authority
makecert -n "CN=MMRootAuthority" -r -sv MMRootAuthority.pvk MMRootAuthority.cer
pvk2pfx -pvk MMRootAuthority.pvk -spc MMRootAuthority.cer -pfx MMRootAuthority.pfx -pi “YourPassword” -po “YourPassword”

Certificate Revocation List
makecert -crl -n "CN=MMRootCRL" -r -sv MMRootCRL.pvk MMRootCRL.crl
pvk2pfx  -pvk MMRootCRL.pvk -spc MMRootCRL.crl-pfx MMRootCRL.pfx -pi “YourPassword” -po “YourPassword”

Server Certificate
makecert -iv MMRootAuthority.pvk -n "CN=MMServerCertificate" -ic MMRootAuthority.cer -sr CURRENTUSER -ss My -sky exchange -eku 1.3.6.1.5.5.7.3.1 -pe  -sv MMServerCertificate.pvk MMServerCertificate.cer
pvk2pfx -pvk MMServerCertificate.pvk -spc MMServerCertificate.cer -pfx MMServerCertificate.pfx -pi “YourPassword” -po “YourPassword”

Client Certificate
makecert -iv MMRootAuthority.pvk -n "CN=MMClientCertificate" -ic MMRootAuthority.cer -sr CURRENTUSER -ss My -sky exchange -eku 1.3.6.1.5.5.7.3.2  -pe  -sv MMClientCertificate.pvk MMClientCertificate.cer
pvk2pfx -pvk MMClientCertificate.pvk -spc MMClientCertificate.cer -pfx MMClientCertificate.pfx -pi “YourPassword” -po “YourPassword”

Viewing certificates
If the command is successful, installed certificates can be viewed by
  1. Open command prompt, type in mmc and press enter.
  2. Select Add/Remove Snap-ins and select Certificates.

Configure IIS

The following article helps on how to enable SSL in IIS.
http://learn.iis.net/page.aspx/144/how-to-set-up-ssl-on-iis/



Here are the steps I did to enable SSL in IIS 
  1. Install the Root Certificate Authority in the Local Machine's Trusted Certificate Store. 
  2. Install the Server Certificate in the Local Machine's Personal Certificate Store. 
  3. Go to IIS and open web site properties/bindings, configure the port for SSL and the certificate to use.
Could not find base address that matches scheme https for the endpoint with binding MetadataexchangeHttpsBinding. Registered base address schemes are [http]

When running from inside Visual Studio, the likely culprit is the project not being configured to use the local IIS Web Server.


Ensure that the service is configured to use the local IIS Web Server under project properties/web tab.

Configure the Username/Password Authentication

I used the following steps to configure username/password authentication
  1. Navigate to configuration/system.serviceModel/bindings/basicHttpBinding/
  2. Add the following lines
    <binding name="usernameHttps">
       <security mode="TransportWithMessageCredential">
          <message clientCredentialType="UserName"/>                   
       </security>                   
    </binding>
  3. Change your endpoint to use the usernameHttps binding
    <endpoint binding="basicHttpBinding"
       bindingConfiguration="usernameHttps" name="BasicHttpEndpoint" contract="WcfService4.IService1" >   
    </endpoint>
    The mexHttpBinding will need to be changed to mexHttpsBinding
    The httpGetEnabled will need to be changed to httpsGetEnabled

  4. Implement a username/password validator by creating a class that inherits from UserNamePasswordValidator
    public class UsernNamePasswordValidator : UserNamePasswordValidator {
        public override void Validate(string userName, string password) {}}
    Ensure that System.IdentityModel and System.IdentityModel.Selectors are referenced.
  5. In the web.config file, goto to configuration/system.serviceModel/behaviors/serviceBehaviors/behavior
  6. Add the following line
    <userNameAuthentication userNamePasswordValidationMode="Custom"
    customUserNamePasswordValidatorType="WcfService4.UserNamePasswordValidator, WcfService4"/>

    The first parameter is the fully qualified name of the custom username/password validator class.
    The second parameter specifies the name of the assembly that the validator is located in.

To audit login attempts a serviceSecurityAudit node can be added
http://msdn.microsoft.com/en-us/library/ms731694.aspx


I used the following configuration.
<serviceSecurityAudit
auditLogLocation="Application"
serviceAuthorizationAuditLevel="Failure"

messageAuthenticationAuditLevel="Failure"
suppressAuditFailure="true" />

When security related problems are encountered, this setting will write to the event log of the machine where the service is hosted.

My web.config file

This is my web.config file with unrelevant sections ommitted
<?xml version="1.0"?>
<configuration>
  <system.serviceModel>   
    <bindings>           
      <basicHttpBinding>     
        <binding name="usernameHttps">
          <security mode="TransportWithMessageCredential">
            <message clientCredentialType="UserName"/>                   
          </security>                   
        </binding>
      </basicHttpBinding>
    </bindings>   
    <services>                 
      <service name="WcfService4.Service1" behaviorConfiguration="DefaultBehavior">       
        <endpoint binding="basicHttpBinding"
          bindingConfiguration="usernameHttps" name="BasicHttpEndpoint" contract="WcfService4.IService1" >   
        </endpoint>
      </service>     
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="DefaultBehavior">
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="Custom"
             customUserNamePasswordValidatorType="WcfService4.CustomUserNameValidator, WcfService4"/>
          </serviceCredentials>
        </behavior>         
      </serviceBehaviors>     
    </behaviors>       
  </system.serviceModel> 
</configuration>

I'll be creating an article on how to consume this service in my next post.

Wednesday, April 11, 2012

Inheriting from a DataRow


I needed to implement a custom data row in one of my projects which made me result to the following code.
Create a custom class which inherit from a DataRow. Add a constructor which accepts a DataRowBuilder.

public class CustomDataRow : DataRow {
   internal CustomDataRow(DataRowBuilder rowBuilder) : base(rowBuilder) { }

}

Override the NewRowFromBuilder method. Inside this method, call the CustomDataRow's constructor passing in the DataRowBuilder.

public class CustomDataTable : DataTable {
   public CustomDataTable(){ }

   protected override DataRow NewRowFromBuilder(DataRowBuilder builder) {

      return new CustomDataRow(builder);

   }
}

This will enable each row created by the CustomDataTable to be of CustomDataRow.

Thursday, March 29, 2012

C# Converting PT0H0M0S to TimeSpan


I was working on importing Microsoft Project data into one of my projects when I encountered a problem where the MSP's TimePhasedData value is using the format PT0H0M0S to represent date values. I needed to convert the PT0H0M0S string to a double representing the total hours. I'm posting this code just in case someone is looking for something similar.

public static double StringToTimeSpan(string source)
   int hours = 0; int minutes = 0; int seconds =0;
   string number = string.Empty ;
   for (int index = 0; index < source.Length; index++) {
      char current = source[index];

      if ("1234567890".Contains(current)) number += current;

      else
{
         if (current.Equals ('H')) hours += int.Parse(number);
         if (current.Equals ('M')) minutes += int.Parse(number);
         if (current.Equals ('S')) seconds += int.Parse(number);
         number = string.Empty ;
      }
   }
   return new TimeSpan (hours, minutes, seconds).TotalHours;

}

Tuesday, March 20, 2012

ComponentOne FlexGrid columns with checkboxes

When working with ComponentOne's FlexGrid bounded to a boolean column, setting the Editor property of that boolean column will make it difficult to change the value of the column.

DataTable table = new DataTable();
table.Columns.Add("Selected", typeof(bool));
table.Columns.Add("Name", typeof(string));
table.Rows.Add(false, "Row 1");
flexGrid.DataSource = table;Column column = flexGrid.Cols[0];
column.Editor = new CheckBox();

In the example above, once the editor property of the column is set to an instance of a checkbox, changing the value of the selected column will take several tries.

I just skipped the setting the editor for boolean columns as a result and let C1FlexGrid manage it. If anyone knows of an explanation or a better way to set an editor of a C1FlexGrid column, just send me a message.


Friday, February 3, 2012

Ideablade License Exceptions when running Applications using DevForce


When running code in Visual Studio which uses Ideablade Devforce, you might encounter the following exception.


An error occurred while processing your product license: This product is not registered or the key may no longer be valid. Please contact your IdeaBlade representative to obtain a new key or email sales@ideablade.com.
Check the messages in the debuglog preceding this error for more information, since this error can occur in some high-security configurations. If your product key is no longer valid or you cannot resolve the problem, please contact IdeaBlade.

The three most common causes of this error include:
  1. An Invalid License key. When this occurs, contact ideablade and make sure that the license key is valid for the version of DevForce you have installed.
  2. The probing assembly is invalid. Check the Ideblade.config file and ensure that the probing assembly points to the assembly that contains your entity model.

  3. A change in one of the files that is used by the Ideablade license logic. When this occurs, simply regenerate the model. That is, open the Ideablade Object Mapper and save.
    The Ideablade license information is found in EntityRelations.cs. If the Ideblade Devforce assemblies are recently upgraded, regenerating the model ensures that this file contains the valid license information.
    [assembly: IdeaBladeLicense("aseriesofcharacters")] 
These findings are found with help from Ideablade support.

Thursday, January 12, 2012

Aspose.Tasks and Task Types


Task dates may be automatically calculated by the MSP's scheduling engine which may cause a task's start and finish dates to shift depending on the task type setting.


Task task = new Task("Name");
task.Type = TaskType.FixedUnits ;

The following is a very useful post by Chris Woodill explaining each of the TaskTypes:
http://chriswoodill.blogspot.com/2009/03/how-to-use-microsoft-project-task-type.html