DomainKeys Identified Mail (DKIM) library for .Net C#

I've just committed code that I've been working on for a couple of evenings that enables you to DKIM sign System.Net.Mail.MailMessage objects and send DKIM signed emails. Ideally you should sign emails at the last possible stage, perhaps by your mail server, as if your mail server alters certain headers or the body it could make the signing invalid but in some cases it's not possible. I was working on adding DKIM signing to Amazon's Simple Email Server (SES) SendRawEmail service and decided to refractor the code into it's own library.

The code currently has a dependency on Bouncy Castle Cryptography API which is used to calculate the SHA256 signature but I plan to remove this once I get it working with my own code.

You can download the code at https://github.com/dmcgiv/DKIM.Net


 

Posted by Damien McGivern with 5 comment(s)
Filed under:

Keyset does not exist error - WCF - IIS7 - SSL

When I was deploying a website that used WCF services I received the following error when browsing to one of the service files to check that it had been configured correctly.

Keyset does not exist

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Security.Cryptography.CryptographicException: Keyset does not exist


Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace:

This error is caused by the account that ASP.Net was running under not having access to the private key of the SSL certificate.

 

To give ASP.Net the require permissions.

1) Launch Microsoft Management Consol (MMC) - run mmc

2) Load certificates : File - Add/Remove Snap-in, Select certificates, Click Add, Select Computer Account, Select local

3) Open - Certificates - Personal - Certificates

4) Right click on the certificate - All Tasks - Manage Private Keys

5) Add Network Service (ASP.Net runs under this account on Windows 2008) 

Posted by Damien McGivern with 1 comment(s)
Filed under: , ,

Amazon Web Services (AWS) Simple Email Service (SES) .Net Wrapper C#

 Amazon recently launched a new email sending service called Simple Email Service (SES) http://aws.amazon.com/about-aws/whats-new/2011/01/25/introducing-amazon-simple-email-service/

 Although there were no examples or code for .Net so I've created a wrapper which you can get at https://github.com/dmcgiv/AwsSesWrapper.Net

Posted by Damien McGivern with 1 comment(s)

Event Update for 7th Dec

At the last event it was mentioned that they may be an event tonight at the Wellington Park Hotel. Just to confirm there will NOT be any event tonight and we’re looking to organise one soon. We will always confirm events with follow up emails and postings on the blog/site.
Posted by Damien McGivern with no comments

Moving to VS2010 caused build server to break - errorMSB3454, MSB3454

 Last night I migrated one of our solutions which contained many dlls projects and a couple of websites from VS2008 SP1 to VS2010, after a few changed to the website configuration the solution was building as expected on my machine.

Committing the changes forced a new build on our build server (CruiseControl.NET) running on Windows Server 2008 64Bit which shortly failed with the following error:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets

errorMSB3454: Tracker.exe is required to correctly incrementally generate resources in some circumstances, such as when building on a 64-bit OS using 32-bit MSBuild. This build requires Tracker.exe, but it could not be found. The task is looking for Tracker.exe beneath the InstallationFolder value of the registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SDKs\Windows\v7.0A. To solve the problem, either: 1) Install the Microsoft Windows SDK v7.0A or later. 2) Install Microsoft Visual Studio 2010. 3) Manually set the above registry key to the correct location. Alternatively, you can turn off incremental resource generation by setting the "TrackFileAccess" property to "false".

Searching lead me a post by Brad Wilson Working Around Build Error MSB3454 which I didn't try but instead tried the solution given in one of the comments which linked to Andy Pook's post Building without Visual Studio 

A quick summary of the post:

1 Install Microsoft Windows SDK v7.1 (We already had it installed in preparation for the move to VS2010 assuming it would just work)

2 Start Windows SDk 7.2 Command Prompt and enter the following

cd Setup

WindowsSdkVer –version:v7.1

3 Also remember to copy the "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0" folder to the build machine if you want to build web projects.

MSBuild now used the 7.1 SDK when building VS solution files instead of searching for 7.0A SDK, which is only installed when you install VS2010, and our build server is working again.

 

Detecting ASP.Net AJAX postback - AJAX History

If you are using ASP.Net AJAX History on a page and the user's browser has JavaScript disabled you may encounter the error:

 A history point can only be created during an asynchronous postback.

To avoid this error always check the ScriptManager's IsInAsyncPostBack is true before saving your AJAX history during the postback.Below is an example of a method within the Page's code behind.

 

void SaveAjaxHistory()
{
	var sm = ScriptManager.GetCurrent(this);

	if (sm == null || !sm.IsInAsyncPostBack)
	{
		return;
	}

	sm.GetAjaxHistory().AddHistoryPoint("name", "value");

}

 

I wrap my AJAX History code in a separate component so I don't directly use the ScriptManager within the Pages code behind so I use the couple of extension methods below for detecting if the request is an ASP.Net AJAX postback or just a regular postback. 

 

using System.Web;
using System.Web.UI;

public static class AjaxPostbackExtension
{

	/// 
	/// Gets a value indicating if the request is the result of an ASP.Net AJAX postback.
	/// 
	public static bool IsAjaxPostback(this HttpRequest request)
	{
		return request.Headers["X-MicrosoftAjax"] == "Delta=true";

	}

	/// 
	/// Gets a value indicating if the page is the result of an ASP.Net AJAX postback.
	/// 
	public static bool IsAjaxPostback(this Page page)
	{
		var sm = ScriptManager.GetCurrent(page);
		return (sm != null && sm.IsInAsyncPostBack);
			
	}
}


Posted by Damien McGivern with 5 comment(s)
Filed under: ,

PostBackUrl not working? Make sure JavaScript is enabled.

 Seems obvious now but this one had me scratching my head for an hour this morning. The web application in question downgrades gracefully if JavaScript isn't enabled everywhere else except for a couple of pages were we were using PostBackUrl on a button control to get around an issue that required that we use no query strings on the page it was redirecting to and we couldn't use session state. I was unaware that this feature required JavaScript on the client until today.


You might be asking yourself - who has JavaScript disabled these days? - well some companies lock down their internal computers for security reasons and one of the lockdowns in this case was disabling JavaScript for IE.


Thankfully I was able to solve the issue by reworking each of the pages so that a POST request wasn’t required anymore and just used a hyperlink. If that hadn’t have been the case I’d have had to create a html form outside of the ASP.Net form to get it to work without JavaScript.

Posted by Damien McGivern with no comments

Special offer from O'Reilly for NIMTUG members

Along with your 35% discount off print books, you can now get 45% off all ebooks you purchase direct from oreilly.com for a limited time.

When you buy an O'Reilly ebook you get lifetime access to the book, and whenever possible we make it available to you in four, DRM-free file formats--PDF, .epub, Kindle-compatible .mobi, and Android ebook--that you can use on the devices of your choice. Our ebook files are fully searchable, and you can cut-and-paste and print them. We also alert you when we've updated the files with corrections and additions.

Just use code DSUG when ordering online at www.oreilly.com/store

Posted by Damien McGivern with 2 comment(s)
Filed under: ,

SQL Error 30080 - The full-text population on table [table name] cannot be started because the full-text catalog is importing data from existing catalogs.

Earlier today I received an error while running an update script on a client's SQL Server 2008 database. The script was trying to add FTS to a new field in the table

The error code 30080 had the message The full-text population on table [table name] cannot be started because the full-text catalog is importing data from existing catalogs. After the import operation finishes, rerun the command.

I search the web but didn't get much help as the only solution I could find involved restarting the SQL Server which wasn't an option. Also there wasn't any additional help in the SQL logs, I tried restoring the database and updating again (only same issue occurred) and even waited for an hour then trying the script again.

In the end my solution was to: 

 

In SQL Server Management Studio open Database > Storage > Full Text Catalogs > Right click Properties 

In the general setting select 'Rebuild catalog' and click OK.

Then I can run the script without errors.

 

I'm not sure why this issue occurred in the first place but at least the database is updated and running as expected now. Hope this helps anyone with the same issue.


 

Posted by Damien McGivern with 1 comment(s)
Filed under: ,

UK Students to get Windows 7 for £30

From 1st Oct Microsoft are offering UK students with a valid uni/college email address either Windows 7 Home Premium or Windows 7 Professional for only £30

For more information see http://www.microsoft.com/uk/windows/studentoffer/

 

Posted by Damien McGivern with no comments
Filed under: , ,

Google Page Speed - Firefox/Firebug Add-On - Web Developer Tool

Yesterday google announced some new tools and services for web developers

We've just added new tools to the suite:

  • Web Elements allows your customers to enhance their websites with the ease of cut-and-paste. Webmasters can provide maps, real-time news, calendars, presentations, spreadsheets and YouTube videos on their sites. With the Conversation Element, websites can create more engagement with their communities. The Custom Search Element provides inline search over your own site (or others you specify) without having to write any code and various options to customize further.
  • Page Speed allows webmasters to measure the performance of their websites. Snappier websites help users find things faster; the recommendations from these latency tools allow hosters and webmasters to optimize website speed. These techniques can help hosters reduce resource use and optimize network bandwidth.
  • The Tips for Hosters page offers a set of tips for hosters for creating a richer website hosting platform. Hosters can improve the convenience and accessibility of tools, while at the same time saving platform costs and earning referral fees. Tips include the use of analytics tools such as Google Analytics to help webmasters understand their traffic and linguistic tools such as Google Translate to help websites reach a broader audience.

I'm impressed by Page Speed which is a Firefox/Firebug add-on for web developers. If you are familiar with Yahoo's YSlow (if not you should be) which is also a FF add-on it offers similar features in that it enables you to analyse the loading of web pages, rates how good the load time is and suggests how you could improve page load times.

A couple of features that I like that are not available in YSlow are:

Use Efficient CSS Selectors - lists those CSS selectors that may slow down the user's experience

 

 

 

Remove unused CSS - lists all the CSS that is not used in a page, great for helping you to remove CSS that is no longer used or refractor large CSS files.

 

 

 

Page Speed Activity - enables you to record your activity on a website over multiple pages and see what the browser is doing and spending time on.

 

 

I highly recommend checking out this add-on - I think it's a great tool.

 

kick it on DotNetKicks.com Shout it
Posted by Damien McGivern with 32 comment(s)
Filed under: ,

WCF CommunicationObjectFaultedException " cannot be used for communication because it is in the Faulted state" MessageSecurityException "An error occurred when verifying security for the message"

I've just spent a couple of hours trying to track down a customer issue with one of our WCF services. Below was part of the unit test I was suing to test the service.

using (var client = new MemberServiceClient())
{
    client.ClientCredentials.UserName.UserName = ConfigurationManager.AppSettings["username"];
    client.ClientCredentials.UserName.Password = ConfigurationManager.AppSettings["password"];
    client.CreateMember(mem);
    client.DeleteMember(mem.ExternalRef);
}

and the exception it was throwing was:

failed: System.ServiceModel.CommunicationObjectFaultedException : The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.
	
	Server stack trace: 
	at System.ServiceModel.Channels.CommunicationObject.Close(TimeSpan timeout)
	
	Exception rethrown at [0]: 
	at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
	at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
	at System.ServiceModel.ICommunicationObject.Close(TimeSpan timeout)
	at System.ServiceModel.ClientBase`1.System.ServiceModel.ICommunicationObject.Close(TimeSpan timeout)
	at System.ServiceModel.ClientBase`1.Close()
	at System.ServiceModel.ClientBase`1.System.IDisposable.Dispose()

When I debugged the code the code actually fails at the CreateMember line not at the close method indicated at by the stack trace. Then I realised that this was one of thoes WCF silly moments - don't use using blocks!. Yes believe it or not if you use a using block around a WCF client and it fails when the using block calls the dispose method it throws a new excpetion masking the real exception.

 

The code below is some helper methods we use for making WCF clients are cleaned up correctly.

 

/// <summary>
/// WCF proxys do not clean up properly if they throw an exception. This method ensures that the service proxy is handeled correctly.
/// Do not call TService.Close() or TService.Abort() within the action lambda.
/// </summary>
/// <typeparam name="TService">The type of the service to use</typeparam>
/// <param name="action">Lambda of the action to performwith the service</param>

public static void Using<TService>(Action<TService> action)
	where TService : ICommunicationObject, IDisposable, new()
{
	var service = new TService();
	bool success = false;
	try
	{
		action(service);
		if (service.State != CommunicationState.Faulted)
		{
			service.Close();
			success = true;
		}
	}
	finally
	{
		if (!success)
		{
			service.Abort();
		}
	}
}

 

Changing the code to using our service helper methods.

 

ServiceHelper.Using(
    client =>

        {
            client.ClientCredentials.UserName.UserName = ConfigurationManager.AppSettings["username"];
            client.ClientCredentials.UserName.Password = ConfigurationManager.AppSettings["password"];
            client.CreateMember(mem);
            client.DeleteMember(mem.ExternalRef);
        }
    );

 

Now the code exposed the actual exception that was causing the issue.

 

failed: System.ServiceModel.Security.MessageSecurityException : An unsecured or incorrectly secured fault was received from the other party. See the inner FaultException for the fault code and detail.
  ----> System.ServiceModel.FaultException : An error occurred when verifying security for the message.
	
	Server stack trace: 
	at System.ServiceModel.Channels.SecurityChannelFactory`1.SecurityRequestChannel.ProcessReply(Message reply, SecurityProtocolCorrelationState correlationState, TimeSpan timeout)
	at System.ServiceModel.Channels.SecurityChannelFactory`1.SecurityRequestChannel.Request(Message message, TimeSpan timeout)
	at System.ServiceModel.Security.SecuritySessionSecurityTokenProvider.DoOperation(SecuritySessionOperation operation, EndpointAddress target, Uri via, SecurityToken currentToken, TimeSpan timeout)
	at System.ServiceModel.Security.SecuritySessionSecurityTokenProvider.GetTokenCore(TimeSpan timeout)
	at System.IdentityModel.Selectors.SecurityTokenProvider.GetToken(TimeSpan timeout)
	at System.ServiceModel.Security.SecuritySessionClientSettings`1.ClientSecuritySessionChannel.OnOpen(TimeSpan timeout)
	at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
	at System.ServiceModel.Channels.ServiceChannel.OnOpen(TimeSpan timeout)
	at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
	at System.ServiceModel.Channels.ServiceChannel.CallOnceManager.CallOnce(TimeSpan timeout, CallOnceManager cascade)
	at System.ServiceModel.Channels.ServiceChannel.EnsureOpened(TimeSpan timeout)
	at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
	at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
	at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)


This exception had me stumped as I had never come across it before but after a bit of digging I discovered that the server time was 7 minutes late and that changing the time fixed the issue. Still trying to figure out how the server's time got out of synced.

Posted by Damien McGivern with 16 comment(s)
Filed under: ,

XML Linq requires correct namespace for all XElements

I've been teaching myself ASP.Net MVC during any free time I get at the weekends. I started a simple test project which has developed into the events section on NIMTUG. Yesterday I was looking for a way to easily create search engine sitemaps for the event and sepaker pages and I found Robert Tennyson's post Dynamic sitemaps with ASP.NET MVC which seems very easy indeed. With a couple of changes I had the sitemaps working and so submitted them to google only to be notified a couple of hours later that they had failed with the error "Invalid Namespace".

I should have validated the outputed Xml using the Google Sitemap Validator but instead I had only tested the xml output in the browser which hid the root namespace (xmlns attribute) but more importantly also hid an empty namespace on all the url elements.

 

<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url xmlns="">
    <loc>http://nimtug.org/events/speaker/details/41</loc>
   <lastmod>2009-05-23</lastmod>
  </url>
  <url xmlns="">
    <loc>http://nimtug.org/events/speaker/details/23</loc>

    <lastmod>2009-05-23</lastmod>
  </url> 

 

It appears that with LINQ to XMl you have to add the correct namespace to every XmlElement which seems like over kill to me as the child elements are supposed to inherit their namespace from their parent (as you would expect) - if anyone knows of an easier way please let me know. My fix was to just add the namespace to each XElement constructor.

protected string GetAlsoluteUrl(object routeValues)
{
    var values = new RouteValueDictionary(routeValues);
    var context = new RequestContext(HttpContext, RouteData);

    string url = RouteTable.Routes.GetVirtualPath(context, values).VirtualPath;

    return new Uri(Request.Url, url).AbsoluteUri;
}

protected ActionResult CreateSiteMap<TModel, TID>(IEnumerable<TModel> items, Func<TModel, TID> getID,
                                                  string controllerName, string actionName)
{
    // XML LINQ requires that ALL XElements have the correct namespace 
    XNamespace xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9";

    var root = new XElement(xmlns + "urlset");
    foreach (TModel item in items)
    {
        object routeValues =
            new
                {
                    id = getID(item),
                    controller = controllerName,
                    action = actionName
                };


        // only add last modified element if we can get the date
        XElement lastMod = null;
        var lm = item as IObjectTimes;
        if (lm != null)
        {
            lastMod = new XElement(xmlns + "lastmod", (lm.Updated ?? lm.Created).ToString("yyyy-MM-dd"));
        }
        root.Add(new XElement(xmlns + "url",
                              new XElement(xmlns + "loc", GetAlsoluteUrl(routeValues)), lastMod
                     )
            );
    }


    using (var memoryStream = new MemoryStream())
    {
        using (var writer = new StreamWriter(memoryStream, Encoding.UTF8))
        {
            root.Save(writer);
        }

        return Content(Encoding.UTF8.GetString(memoryStream.ToArray()), "text/xml", Encoding.UTF8);
    }
}
Posted by Damien McGivern with 1 comment(s)
Filed under: , ,

PDF SQL Server Cheat Sheet

MVP Pinal Dave has posted his SQL Server cheat sheet as a PDF download.

Posted by Damien McGivern with no comments
Filed under:

My Phone public beta opens

Microsoft has opened the beta of the My Phone service to the public. Only Windows Mobile phones with version 6 and up are supported.

It allows you to backup you data and access it from you My Phone web account and more

Microsoft My Phone

Posted by Damien McGivern with no comments
Filed under:
More Posts Next page »