Microsoft Article : Simple Async Await Example for Asynchronous Programming

Simple Async Await Example for Asynchronous Programming
Simple Async Await Example for Asynchronous Programming

I have written another article for Microsoft called Simple Async Await Example for Asynchronous Programming. The article walks through a simple example to help you get your head around using Async and Await.

Advertisements

Microsoft Article : Using Code Metrics to Guide Code Reviews

Using Code Metrics to Guide Code Reviews
Using Code Metrics to Guide Code Reviews

This week I have published a new article on the Microsoft Developer Blog. The article is called “Using Code Metrics to Guide Code Reviews” and talks about how you can use the code metrics in Visual Studio as a way to run a code review to stimulate discussions about code quality.

The article also takes a look at the NDepend tools and how they can give you an ever deeper insight into what’s happening in your code.

Microsoft Article : How to Securely Store Passwords and Beat the Hackers

How to Securely Store Passwords and Beat the Hackers
How to Securely Store Passwords and Beat the Hackers

I have just written my first article for the Microsoft UK Developer site on How to Securely Store Passwords and Beat the Hackers. The article talks about the best way to protect passwords by first exploring ways that you shouldn’t protect and store passwords.

Update a WPF UI from Another Thread

This is a shorter post with a small solution to a problem, but I wanted to add it here for my own reference. I have recently been working on a little WPF pet project as I want to learn XAML and WPF. I seemed to miss that generation of UI technology when I went from mainly doing WinForms work into WCF and back end enterprise development.

The issue I had the other day was that I had a timer running in my code that triggers an event when the elapsed time hits a certain time. From that event handler I wanted to update something on the user interface. If I update that UI item directly from the event I got the following exception being thrown. This is because the UI is operating on a different thread to the thread handling the timer event.

Thread Exception
Thread Exception

The solution is to use the Dispatcher.Invoke( Action ) method to make the call to the UI thread. This is demonstrated in the following example. We have a timer being setup with an event (OnTimedEvent) being fired every 5 seconds. When the OnTimedEvent is called, the UI is updated inside the Dispatcher.Invoke( Action ) method.

private Timer _timer = new Timer();
_timer = new Timer(5000);      
_timer.Elapsed += OnTimedEvent;
timer.Enabled = false;

private void OnTimedEvent( Object source, ElapsedEventArgs e)
{
     Dispatcher.Invoke(() =>
     {
          // Set property or change UI compomponents.              
     }); 
}

MSDN describes this solution as follows:

In WPF, only the thread that created a DispatcherObject may access that object. For example, a background thread that is spun off from the main UI thread cannot update the contents of a Button that was created on the UI thread. In order for the background thread to access the Content property of the Button, the background thread must delegate the work to the Dispatcher associated with the UI thread. This is accomplished by using either Invoke or BeginInvoke. Invoke is synchronous and BeginInvoke is asynchronous. The operation is added to the event queue of the Dispatcher at the specified DispatcherPriority.

Invoke is a synchronous operation; therefore, control will not return to the calling object until after the callback returns.

Practical Cryptography in .NET Course Released by Pluralsight

I am pleased to announce that my latest course, Practical Cryptography in .NET has been released by Pluralsight.

The course description is as follows:

As a software developer you have a duty to your employer to secure and protect their data. In this course you will learn how to use the .NET Framework to protect your data to satisfy confidentiality, integrity, non-repudiation and authentication.

This course covers random number generation, hashing, authenticated hashing and password based key derivation functions. The course also covers both symmetric and asymmetric encryption using DES, Triple DES, AES and RSA. You then learn how to combine these all together to product a hybrid encryption scheme which includes AES, RSA, HMACS and Digital Signatures.

The course is aimed at teaching developers about the importance of protecting sensitive data within their systems.

Practical Cryptography in .NET Coming Soon to Pluralsight
Practical Cryptography in .NET Coming Soon to Pluralsight

As-well as giving lots of technical background, the course will be very practical with lots of live code demonstrations. The course will be split into the following modules.

1. Course Outline and Introduction
2. Cryptographic Random Numbers
3. Hashing Algorithms
4. Secure Password Storage
5. Symmetric Encryption
6. Asymmetric Encryption
7. Hybrid Encryption
8. Digital Signatures
9. Secure String
10. Course Summary

Modules 2 – 6 covers a lot of theory and practical advice on using what is built into the .NET Framework. Module 7 on Hybrid Cryptography takes this a step further to combine a lot of the cryptographic primitives discussed into a cryptography scheme that gives the flexible key management benefits of RSA with the benefits and speed of algorithms like AES which includes full authenticated integrity checking.

Practical Cryptography in .NET Coming Soon to Pluralsight
Practical Cryptography in .NET Coming Soon to Pluralsight

This then gets expanded on further by introducing the concept of Digital Signatures to build in non-repudiation into the system.

Practical Cryptography in .NET Coming Soon to Pluralsight
Practical Cryptography in .NET Coming Soon to Pluralsight

The course has been a lot of fun to produce and I hope you find it useful. Protecting data is something that every developer should take very seriously and this course gives you all the tools you need to protect your companies data from ex-filtration by hackers or anyone else that wants to cause organisations harm.

Generating Data Type Classes from XML

I was shown a nice trick recently in Visual Studio that I never realised existed. The technique is an easy way to create a C# class from an XML or JSON document. Have you ever been in the situation where you are taking an XML or JSON feed from one system and need to integrated it into a project you are working on? I have plenty of times and creating the data type from that XML so you can de-serialize it back can be a pain.

Generating Data Type Classes from XML
Generating Data Type Classes from XML

At first I thought I must be the only person who doesn’t know this, but on discussing it with some team members, they hadn’t hear of it either. Lets take a look at an example with XML. I have taken an example excerpt of XML from the W3CSchools website.

<breakfast_menu>
    <food>
        <name>Belgian Waffles</name>
        <price>$5.95</price>
        <description>
            Two of our famous Belgian Waffles with plenty of real maple syrup
        </description>
        <calories>650</calories>
    </food>
    <food>
        <name>Strawberry Belgian Waffles</name>
        <price>$7.95</price>
        <description>
            Light Belgian waffles covered with strawberries and whipped cream
        </description>
        <calories>900</calories>
    </food>
    <food>
        <name>Berry-Berry Belgian Waffles</name>
        <price>$8.95</price>
        <description>
            Light Belgian waffles covered with an assortment of fresh berries and whipped cream
        </description>
        <calories>900</calories>
    </food>
    <food>
        <name>French Toast</name>
        <price>$4.50</price>
        <description>
            Thick slices made from our homemade sourdough bread
        </description>
        <calories>600</calories>
    </food>
    <food>
        <name>Homestyle Breakfast</name>
        <price>$6.95</price>
        <description>
            Two eggs, bacon or sausage, toast, and our ever-popular hash browns
        </description>
        <calories>950</calories>
    </food>
</breakfast_menu>

If you take the above XML and place it into the clip board. Then create or open an existing visual studio project. Goto the Edit Menu and select “Paste Special” and then “Paste XML as Classes“. This will create a set of classes that represents the XML data structure.

Cryptography in .NET Talk : Slides and Sample Code

Cryptography in .NET Talk at Dot Net Notts
Cryptography in .NET Talk at Dot Net Notts

Today I did a talk on Cryptography in .NET. The talk synopsis is as follows :

Data security is something that we as developers have to take seriously when developing solutions for our organizations. Cryptography can be a deeply complicated and mathematical subject but as developers we need to be pragmatic and use what is available to us to secure our data without disappearing down the mathematical rabbit hole.

Cryptography in .NET Talk at the DotNet Notts User Group by Stephen Haunts
Cryptography in .NET Talk at the DotNet Notts User Group by Stephen Haunts

In this talk Stephen Haunts will take you through what is available in the .NET framework for enterprise desktop and server developers to allow you to securely protect your data to achieve confidentiality, data integrity and non-repudiation of exchanged data. Stephen will cover the following:

  • Cryptographically secure random number generation
  • Hashing and Authenticated Hashes
  • Symmetric Encryption with DES, TripleDES, and AES
  • Asymmetric Encryption with RSA
  • Hybrid Encryption by using Symmetric and Asymmetric encryption together.
  • Digital Signatures

You can now download the Slides and Sample Code in C# from this talk. If you have any questions about the talk or the subject matter then please feel free to leave a comment on this article or contact me via the contact form on this blog.