Encrypting and Decrypting Web.Config sections

Another little personal reminder, to save a bigger google search next time:

Encrypting web Config

  1. Run CMD in admin mode
  2. Add .net folder to path, (if not already there): set path=%PATH%;C:\Windows\Microsoft.NET\Framework\v4.0.30319
  3. copy web.config web.config.backup
  4. aspnet_regiis -pef appSettings .

Decrypting web config

  1. aspnet_regiis -pdf appSettings .

SQL Server: Finding a column in multiple databases.

My client has many instances of the same database in various versions. The version I’m developing against is missing a field expected by a c# class, and I can’t find a script to create the column.

I came up with the following SQL Server query to allow me to search all the instances of the database to find one that contained the field.

EXEC sp_MSforeachdb 
 'SELECT ''Found in ?''
 FROM sys.columns 
 WHERE Name = N''FieldName''
 AND Object_ID = Object_ID(N''TableName'')'

Replace the field name and the table name with the values you’re looking for and it will search all the databases on the server for  a match.

Launching .EXE applications from a browser

My client has an Angular intranet portal for their main c# WinForm application. They have a requirement to launch the main application from the portal website.

There is a fairly easy way to go about this in Windows although it does require adding an entry to the registry, to set up a URI Scheme. Here is an example to load notepad.exe

      (Default) = "URL:jmNotepad Protocol"
      URL Protocol = ""
         (Default) = "notepad.exe,1"
               (Default) = "C:\windows\notepad.exe"

In the web page, add a simple link:

<a href="jmNotepad:">Launch Notepad</a>

The first time it’s launched you get a warning, which you can click to ignore subsequently.

Automatically building NuGet packages for your project

I’ve been using the Nuget Package Explorer for ages, but recently found out how to automatically build  the NuGet for a project as part of your normal build.

  1. Enable NuGet Package Restore  for the solution. This will create the .nugget folder with NuGet.exe and a configuration file.
  2.  You need to edit the .csProj file and add the following lines:
    <Target Name="AfterBuild" Condition=" '$(Configuration)' == 'Release'">
       <GetAssemblyIdentity AssemblyFiles="$(TargetPath)"> 
          <Output TaskParameter="Assemblies" ItemName="AssemblyVersion" /> 
       <Exec Command="echo %(AssemblyVersion.Version)" /> 
       <Message Text="Released %(AssemblyVersion.Version)" Importance="high" /> 
       <Exec Command="nuget pack TG.DocumentManagement.RetrievalIntegration.csproj -Version %(AssemblyVersion.Version)">
  3. Create a .nuspec file:
    <?xml version="1.0"?>
    <package >
        <releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
      <copyright>Copyright ©SilverCode Solutions Limited 2016</copyright>
        <tags>SilverCode  Project blah blah blah</tags>
       <file src="web.config.transform" target="content/web.config.transform"/>
  4. Reload the project, every build should create a nugget package for in the output folder.

Login redirection using AngularJS & UI-Router



When a user follows a link on an email to the site, but needs to login before they can view the page, you need to remember the requested page and return to it after a successful login.

In the following code examples, I’m using an AuthenticationService which handles user login, and stores authentication state.

First on the landing page, you need to store the current router state and the params needed to get back to the specific page.

if (!AuthenticationService.isAuthenticated) {
$cookies.loginDestination = $state.$current.name;
$cookies.loginParams = { id: $stateParams.id};

In the AuthenticationService, after a successful login:

var loginDestination = $cookies.loginDestination || '/';
var loginParams = $cookies.loginParams || null;
$cookies.loginDestination = null;
$cookies.loginParams = null;
$state.transitionTo(loginDestination, loginParams);

Facebook Spam

Facebook are filling my phone app with so many updated from pages I haven’t liked, that the app is becoming useless. They obviously know I’m a software developer, but this is terrible spamming. So much that I don’t see updates that I actually want to see from friends and pages that I have liked.

Today’s updates from unliked pages:
O’Reilly Media
at&t developer program *13
Atmel Corporation
Smashing Magazine * 4
Lean startup circle
OMG! Chrome * 2
The Hacker News * 4
SQL Server Geeks *
Media Temple
SQL Authority

Entity Frameworks, WebApi, JSON.Net and Serializing Circular References – Better fix

As an improvement on yesterday problem and solution, there is a simple change to the JSON.Net settings which solves this issue, just add this into Global.asax:

JsonSerializerSettings settings = new JsonSerializerSettings
    PreserveReferencesHandling = PreserveReferencesHandling.Objects,
    Formatting = Formatting.Indented
HttpConfiguration config = GlobalConfiguration.Configuration;
config.Formatters.JsonFormatter.SerializerSettings = settings;

Entity Frameworks, WebApi, JSON.Net and Serializing Circular References

While Entity Frameworks handles circular references between models very well, putting them through a WebApi web service and serializing them, Json.Net starts throwing errors.

The quickest way around this is to rebuild the objects and miss out the references from the child back to the parent.

public virtual HttpResponseMessage Get()
var data = ADRepository.GetAllocatedApplications();

// do this to get around the bloody annoying Json Circular References Issue
List result = data.Select(x =&gt; new ActiveDirectoryGroup
Id = x.Id,
Name = x.Name,
IsDeleted = x.IsDeleted,
LastUpdated = x.LastUpdated,
LastUpdatedBy = x.LastUpdatedBy,
Applications = x.Applications.Select(y =&gt; new Application
Id = y.Id,
Name = y.Name,
CommandLine = y.CommandLine,
IsDeleted = y.IsDeleted,
LastUpdated = y.LastUpdated,
LastUpdatedBy = y.LastUpdatedBy

return Request.CreateResponse&lt;List&gt;(System.Net.HttpStatusCode.OK, result);

Microsoft ALM

I was very impressed overall by the latest product suite and the improvements to our workflow it would enable.

While we as the web development team already use TFS for version control, and are starting to look at it for user stories and task tracking, the possibilities of having it manage all the complete workflow from requirements and specifications, through the development process, then testing, and finally the release process. Keeping the requirements linked to the tasks, the source code, and the tests.

 Let’s hope we can start using this fully in the near future.