Posts

Showing posts from February, 2014

.Net Winforms Basic BackgroundWorker Example

I use a BackgroundWorker a lot when working with WinForms in order to perform database or webservice calls without freezing the UI thread. Of course web services can be called using their built in asynchronously methods however when you have a separate data access or model layer, these asynchronous methods are not readily available.  On a new project I was working on, I was looking for a basic example online for quick reference but there wasn't one amongst the top search results so I thought I'd post one. using System.ComponentModel; using System.Threading; using System.Windows.Forms; public partial class MainForm : Form { private readonly BackgroundWorker backgroundWorker = new BackgroundWorker(); public MainForm() { this.InitializeComponent(); this.backgroundWorker.DoWork += this.BackgroundWorkerDoWork; this.backgroundWorker.RunWorkerCompleted += this.BackgroundWorkerCompleted; t...

Easy .NET MVC Active Directory Attribute Based Authorization

Active Directory based authorization in .NET is fairly easy. Just throw an attribute on a controller as follows: [Authorize (Roles="MyAdGroup")] public class SettingsController : Controller Sometimes though you do not want to hard code a role in an attribute as you may want to add or remove roles at will. You may also want to change the roles based on whether you are in production or not. I like to keep my Active Directory roles either in a database or a web.config file so that others can change authorization on the fly. In order to have greater control over your authorization roles you need to extend the AuthorizeAttribute and override AuthorizationCore. You also need to override HandleUnauthorizedRequest in order to have a custom redirect page. /// <summary> /// Redirects to the unauthorized page. /// </summary> public class AuthorizeSiteRedirect : AuthorizeAttribute { /// <summary> /// Authorization based on roles in...

Common.Net library methods: Querying Active Directory for users and groups

Finding the groups a user belongs to in active directory along with the members of that group is something that comes up a lot when .Net apps use Active Directory for authentication. Here are some common library methods to find members of a group or groups a member belongs to. This code requires a reference to System.DirectoryServices.AccountManagement. using System; using System.Collections.Generic; using System.DirectoryServices.AccountManagement; using System.Linq; public class ActiveDirectoryGateway { private readonly string domain; private readonly ContextType contextType; public ActiveDirectoryGateway(ContextType contextType, string domain) { this.contextType = contextType; this.domain = domain; } /// <summary> /// Retrieves a list of AD groups belonging to an AD User. /// </summary> /// <param name="user">The active directory ...

Common .Net Library Methods: Easy XML Serialization and deserialization

Serializing XML to an object of a given type and back has been very common in a lot of my projects. It may not be allowable when you are working on multiple projects in different groups to import a common class library so I thought I'd post some basic serialization code generic to most projects that I have had to use a lot lately. /// <summary> /// Deserializes an XML document into a given type. /// </summary> /// <typeparam name="T">The type to deserialize.</typeparam> /// <param name="xml"> The xml. </param> /// <returns> An object representative of the XML document. </returns> public T Deserialize<T>(string xml) { var xmlSerializer = new XmlSerializer(typeof(T)); using (var reader = XmlReader.Create(new StringReader(xml))) { if (xmlSerializer.CanDeserialize(reader)) { ...

WCF Logging raw soap requests.

To troubleshoot a contract filter mismatch or other WCF fault message being thrown from  third party consumers, here is the web.config settings that will show the raw soap requests to inspect if the caller is using a malformed request. Please be aware that the first thing to do when troubleshooting a contract filter mismatch is to ensure the web reference is up to date and running. This message can be very deceiving such as if you are calling a web service from a web service, and the latter web service is not running or running on a different virtual etc. Make sure the first section is contained within System.Servicemodel. As you can see I do not show the opening tag tag. <!-------- within the system.servicemodel element --------&gt <diagnostics> <messageLogging logEntireMessage="true" logMalformedMessages="true" logMessagesAtServiceLevel="false" logMessagesAtTranspo...