Archive

Archive for May, 2012

SQL Server 2008 – Filtered Index

May 31, 2012 Leave a comment

Granted that SQL 2012 is already out, there is actually a feature inside SQL 2008 which is especially useful for some form of slice and dice when you have too much data.

 

This feature is called Filtered Index. You can find more information about it at http://technet.microsoft.com/en-us/library/cc280372.aspx.

 

Generally the purpose is to improve query performance by storing indexes only for a subset of the data. Additionally it reduces costs and storage incurred with index maintenance.

 

The syntax to create one is as follows

create unique nonclustered index [IndexName] on dbo.[TableName]([Fields])
where [Conditions]

Advertisements
Categories: SQL Server

Enabling dual date format output for JSON.NET

May 9, 2012 1 comment

 

By default the JSON.Net library will emit all dates using the ISO Date format e.g "2012-05-09T00:00:00Z", but Microsoft’s JSON formatter for date will emit using Microsoft’s own format “new Date(1234656000000)”

 

Problem comes in when you have previously been using Microsoft’s JSON formatter and now want to use JSON.NET’s implementation. You will then have to decide whether you want to use the new format and change all your existing apps, or use the old format and make it slightly less interoperable.

 

One other alternative which I’m going to suggest below is to emit both date types, and suffix the other date property with a “_js” to differentiate between the 2 dates

image

 

To get this to work, just follow the steps below

  1. Download the latest JSON.NET library at http://json.codeplex.com/
  2. Open the solution and goto the Newtonsoft.Json project
    image
  3. Open JsonSerializerInternalWriter.cs
  4. Search for the following function
    private void WriteMemberInfoProperty(JsonWriter writer, object memberValue, JsonProperty property, JsonContract contract, JsonContainerContract collectionContract)

    This function is the primary function that writes out the JSON output based what you passed in to serialize

  5. The function should look something like
    image
  6. What we are targeting is the last 2 lines of the function i.e

    writer.WritePropertyName(propertyName);
    SerializeValue(writer, memberValue, contract, property, collectionContract, null);

  7. Change these 2 lines of code to

    //Ensure membervalue is not null and is a datetime type

  8. if (memberValue != null && memberValue.GetType() == typeof(DateTime))
    {
        //Default property uses Microsoft Date Format
        writer.WritePropertyName(propertyName);
        writer.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;
        SerializeValue(writer, memberValue, contract, property, collectionContract, null);

        //Same property with a _js returns a ISO Date Format
        writer.WritePropertyName(propertyName + "_js");
        writer.DateFormatHandling = DateFormatHandling.IsoDateFormat;
        SerializeValue(writer, memberValue, contract, property, collectionContract, null);
    }
    else
    {
        //Original implementation 
        writer.WritePropertyName(propertyName);
        SerializeValue(writer, memberValue, contract, property, collectionContract, null);
    }

  9. Compile the project and copy the new json.net dll to your server
  10. Test and make sure you see both dates being emitted
Categories: AJAX, ASP.NET

Windows Identity Foundation–Updating an expired issuer certificate

May 3, 2012 Leave a comment

Because Windows Identity Foundation (aka STS) requires a non expired certificate to work, when a certificate expires, you will start getting errors such as

ID4175: The issuer of the security token was not recognized by the IssuerNameRegistry. To accept security tokens from this issuer, configure the IssuerNameRegistry to return a valid name for this issuer.

There are quite a few steps to fix this if you are running your own STS provider

 

For the STS provider

  1. Start up Management Console (mmc.exe)
  2. Add the Certificates Snap In for Computer Account (Add/Remove Snap In, Select Certificates, Computer Account)
  3. Import the new certificate into the Personal\Certificate Branch (right click, import)
    IMPORTANT: Make sure the certificate which you are importing has the private key inside
  4. Type out the thumbprint (Details\Thumbprint).
    IMPORTANT: DO NOT COPY AND PASTE, this will cause the same error to occur

For the STS Consumer

      1. Update the Federation Metadata, using the command below

        "C:\Program Files (x86)\Windows Identity Foundation SDK\v4.0\FedUtil.exe" /u "[PATH to Web.config]" /o "[Path to Log File]" /m "[Federation Metadata Location]"

        eg: "C:\Program Files (x86)\Windows Identity Foundation SDK\v4.0\FedUtil.exe" /u "D:\Websites\sts\web.config" /o "D:\Websites\sts\UpdateLog.txt" /m https://stsprovider/FederationMetadata/2007-06/FederationMetadata.xml

      2. Paste and replace the typed thumbprint from the provider inside the web.config file (microsoft.identitymodel –> service –> issuerNameRegistry –> trustedIssuers)
      3. Save and test
Categories: ASP.NET