Using MVC5 Owin Security with EF Code First

Good evening, I’d like to share my solution for using MVC5 Owin security with VS2013 Update 1.
I wanted to have my own DBContext contain both my own DBSets and also the security tables.

Now I could find zero documentation on this and I actually walked away from the bleeding edge last week and reverted to MVC4. Tonight I had some free time and like a dog with a bone I wanted to figure this out. I’m not saying I came up with the nicest or correct solution, but alas I came up with something that appears to be working for me.
Please feel free to give me any feedback I’d appreciate it.

The problem

I wanted one single database containing my own entities and also the security entities.
I wanted to use the latest MVC5 built on the OWIN (open web interface for .net).
I wanted to use code first migrations to keep my database up to date.
I could find no documentation..

The Solution

  • Create my own entity class



  • Create my own DBContext and IdentityStoreContext (the secret sauce) 

image The SiteCertDbContext derives from the IdentityDbContext used by default in MVC5 Update1.

  • Tell the account controller use this new DbContext



That’s pretty much it,  hope it helps someone else out you can now open the Nuget package manager and Enable-Migrations.

Parameterized Unit Tests with JUnit

I’ve been shown a lovely little utility for testing with JUnit 4.11 whereby one can have a list of items performed as part of the single unit test.

First set add the annotations you see below and set your inputs and expected outputs. (Arrange and Assert)






Then create a constructor and some static variables to store each Tuple






Then define your single unit test (the business logic is your Action)






That’s it, a really nice way to have a single unit test that can be run multiple times with different parameters.







If you would like to do the same sort of testing in .net you have some choices to make.

you could use NUnit and use the TestCaseAttribute






Of if like me you prefer MSTest then you can opt for a data driven unit test.
See this vs2013 page (has worked at least since vs2010) :


Xamarin a few weeks on

  • I’ve gotten my hands dirty,
  • I’ve published v1.0 to the store
  • I’ve gotten the best support I’ve ever seen from Xamarin
  • I’ve gone back to Objective-C, BOOM!

Bindings drove me daft

The reason I walked away was that I found some nice Objective-C widgets I wanted to use. In order to use these in Xamarin I needed to either

  • Port the source to c# (estimated 1 day)
  • Create a library in XCode and a component binding in Xamarin Studio

The first option seemed like an approach I could achieve but did I really want to do this every time I found a nice new control.

The second option proved painful, there were some tools that didn’t work (beta tools of course), I wasn’t quite clear about the multi targeted library I was building etc. It was the straw that broke this camel’s back.

Now I’ve previously indicated that I’m not a seasoned ObjectiveC developer, but I’ve written a few app and some of them even made it to the app store, so for me it didn’t seem daunting…

I had a few obstacles to overcome:

Basic Auth: I’ve posted before when I started my Xamarin rants that it was a little painful to do Basic Auth, I wanted to use blocks as I like this approach and not the delegate approach, but this was a little painfully or at least it meant that I had to write my own classes, what I did in the end was revert to my old friend   AFNetworking that supported exactly what I needed.

KeyStore: The Obj-C code is bloated and verbose and not intuitive, the Xamarin approach is so much easier.

Lessons learnt

Too many options or being a polyglot can be a PIA (and i’m not talking primary interop assembly), If I didn’t know objectiveC then I would have just figured out the Bindings faster and been happy.


What would I recommend to my fellow C# programmers…. Use Xamarin! I haven’t, but I’ve already invested many hours/days/weeks the language and 3rd party libraries,  the mono stack gives you a lot of
this for free and you will be familiar with it from your .net background.

Also the app I’m currently working on is just collecting Json and displaying it, so it’s not very code intensive on the client. If when an Android version is done it will be in Java because I’ve programmed in Java for years too (hey I’m looking for an excuse (and the time) to try this new Android Studio).
Given a different use case I we may well have an even more compelling reason for using C# for both platforms with something like MVVM Cross and achieve a more DRY approach.

Xamarin UIDeviceFamily

I was releasing an app to the the Apple App store tonight (actually two apps, I've done an Atley Hunter ;-) )

One of my apps encountered a problem when I tried to upload my archive:

"This bundle is invalid. The UIDeviceFamily key must be present when requiring a MinimumOSVersion of at least 3.2."

This did confuse me for a while, I did a bit of research and this information is supposed to be set automatically with XCode when I set the target platform.

Given I was using Xamarin Studio I was a little unsure where I stood so i took a shot in the dark.
I changed the platform in Xamarin to Universal and then back to iPhone/iPod problem solved,

hope this tip helps someone

To Xammer or not to Xammer

Xamarin vs XCode

Anyone that reads this blog regularly or knows me, will know that I’m a C#/Java guy primarily, but I do love all other languages, especially javascript and objective-c  (my objective-c is just about passable, mainly because I’ve only written 3 iOS applications). I was in London this week and I got an early preview of a new service, it looked pretty good so I was thinking of how I could write a few of my own clients for this. I’ve the luxury of being in a position to take a few approaches .

Client options

  • Html5 Desktop Client
  • iOS native client
  • Droid native client (I’ve never strictly speaking done one, unless you include my hello world post two years back)
  • PhoneGap/KendoUi iPad/Droid apps (I’m doing the phone apps for my Expenses service with these technologies).

Decisions tree

My first instinct was to go the PhoneGap with KendoUI, I actually did the initial layout and had a look in the Android Emulator and it was pretty nice (as I was in London I didn’t have my MacBookAir with me and I didn’t have the Visual Studio Phone Tools installed either so eclipse/android it was with that painfully slow emulator). The problem I encountered with this approach was that CORS was not enabled by default and I use visual studio to develop design debug (PhoneGap can do CORS once the site is white listed).

My next option was to write the native clients, when I arrived back home I quickly ensured I could connect to the server with basic authentication, I used objective-C firstly, I didn’t use any 3rd party libraries and the end result appears a little verbose (a better iOS developer will probably cry when they look at these screenshots and tell me use AFNetworking or blocks etc (which I used in my companion app).

XCode Version


Make the initial request and delegate to the view class.



Here i set the username and password for the request when/if challenged.

Screen Shot 2013-05-04 at 22.15.41

Here i allocate a place to store the response if it’s was a success and save to data into urlData.


Here I list all the key value pairs in the JSON returned (something I  don’t actually show in the c# version.

The attraction of XCode/Objective-C to me is that it’s a different toy to play with.

MonoDevelop Version

C# is just a fantastic language it’s RAD and Xamarin have done a fantastic job of bringing it to the iOS and Android Platforms. This  screen shot shows the same request to the server as done in XCode, the difference is that this took me about 1 minute where the objective-c version took me about 25 (although i did reuse the storyboard).



You said Decision?

So how will I proceed? I’m going to ditch the objective-C approach for sure:


  • It’s so much faster! I love C++ but can’t get myself to use it for app dev these days simply because C# kicks it to touch for rapid application development, same goes for Objective-C
  • Because I don’t expect to continue this POC myself and will have to hand it over to someone, and I don’t know anyone else on the team that knows objective-C.

I’m actually still leaning towards the PhoneGap/KendoUI option, I just need to get my grubby hands on the server so i can add the CORS headers (again because I don’t want to do the bulk of my debugging with Phone gap but rather IE/Chrome and I quite like my browser stack (jQuery/Knockout/Breeze etc) but the main advantage is that the the same source can then be used for the Android platform. Sure Facebook/LinkedIn etc have all been moving away from html5 because of the tooling, but I feels it’s the best solution for getting to both markets quickly.

So that is my reasoning, I expect everyone with be faced with much the same decisions and will have to weight the pros and cons themselves.

File Upload MCV4 Web API, Knockout.js


I wish to follow up on my previous post Uploading a file in MVC4 C#5 .NET 4.5

I promised a few things here, an Ajax client, WinRT, iOs, Droid, This post addresses the ajax upload.

First some background, I’m working on an expense tracking system at the moment, the core technologies involved in this Single Page Application are:

  • ASP MVC4 WebApi
  • Html5 SPA
  • Knockout.js

A fundamental part of this system is the ability to upload receipts.

When the user browses to an image file, it gets converted to base64 and uploaded via a MVC4 Api controller.

Here are the important parts:



First we create an image where we can display either the previously selected image or the newly selected image.
We only display this image if it’s in the javascript model.

Secondly we bind the html5 input file with a knockout binding.




The important parts are the image and imageType properties, there also exists a computed property that joins these two so it can be displayed in an image tag. The reason i keep these separated is that I can’t post the source as is without further encoding.

Knockout Bindings

In knockout.js you are not limited to the built in bindings like, click and value, you can create your own,
I’ve taken as my start point, this pretty much does what I want, however I made a slight tweak in that i wanted base64. (basically because I’ve written some of the objective-C iOS app already and didn’t fancy changing it).







All source can be viewed @

ASP MVC4 Web API file upload: Unexpected end of MIME


So I’ve had a problem uploading a file using a HTML5 input of type file field.


For love nor money could I see a problem with the code above (in my defence I’m working on this project late in the evening and have my First dose of Man Flu this year, I’m a 2012 survivor see: )

When i get into my server code an exception was getting thrown when i read the multipart post.


All the Googling in the world didn’t help me, I saw lots of people adding “\r\n” which I’m still scratching my head over to be honest, I saw others complain about the MVC4 beta..


But hang on: I’ve done this before: So what has changed? actually something really silly ,

I simply forgot to set the input name attribute!!!


Hope this helps somebody …

Enabling Facebook OAuth in MVC4 SPA


There are two steps, the first step is to register a facebook application, after you register you will have a key and password. The next step will be to insert these into you application.


Step 1.

    Enable OAuth login using Facebook, Twitter

    Steps to get keys for Facebook

    • Go to the Facebook developers site (log in if you're not already logged in).
    • Choose the Create New App button, and then follow the prompts to name and create the new application.
    • In the section Select how your app will integrate with Facebook, choose the Website section.
    • Fill in the Site URL field with the URL of your site (for example, The Domain field is optional; you can use this to provide authentication for an entire domain (such as
      Note   If you are running a site on your local computer with a URL like http://localhost:12345 (where the number is a local port number), you can add this value to the Site URL field for testing your site. However, any time the port number of your local site changes, you will need to update the Site URL field of your application.
    • Choose the Save Changes button.
    • Choose the Apps tab again, and then view the start page for your application.
    • Copy the App ID and App Secret values for your application, here is what it looks like, I’ve blurred my app id and secret.
    • image
    • Exit the Facebook developer site

      Step 2.

      Edit your App_Start/AuthConfig.cs with these new settings



      That’s it, you can no log in with facebook, see the placeholder template below.

JAX-WS, Eclipse, JBoss


Ok another Java post, they are few and far between, but I’ve already polluted this blog with objective-c, javascript and other non .net languages so why not.

So I was lying in bed last night my wife was hogging the windows machine watching some film or other, so I’d a choice between reading 50 shades of grey or firing up my mac book air, no contest there…

I recently interviewed a guy that had moved from Apache Axis to JAX-WS, the way he described it sounded a lot like WCF (windows communication foundation) so I wanted to see for myself.


  • Install Jboss 7.1.1 for an application server
  • Install Eclipse juno IDE for Java
  • Install Mono Develop (not necessary but i had this already for iPhone dev so thought what the heck I’ll use it for the client)

So what is JAX-WS? The Java API for XML Web Services (JAX-WS) is a Java programming language API for creating web services. It is part of the Java EE platform from Sun Microsystems. Like the other Java EE APIs, JAX-WS uses annotations. Here’s how I created a sample one.

Ensure JBoss can run

Start the standalone shell script and check you can see http://localhost:8080 page below in your browser


Choose JavaEE perspective in Eclipse


Create a new project in Eclipse  (dynamic web)


Add the following webservice class

Complete with annotations


Modify web.xml

Add the highlighted section


Configure the Local JBoss server in eclipse

Right click on the server you added and choose Add/Remove


Add your deployment


Add your current deployment

Start Application Server

Click on the Play button in the server tab toolbar, you should be automatically switched to the Console pane in Eclipse. Take note that your DynamicTest war file is deployed.


Review the JBoss Admin Console

Specifically the Webservice Endpoints, You should see your webservice deployed here.


You can also browse to the wsdl


Create your client

I used C# with the Mono Develop IDE to create a simple Console Application


Just add a Webservice the way you would in Visual Studio (I went for .net 2.0 WS because the WCF version didn’t create an app.config for me (visual studio you spoil me)).



And that’s it your first JAX-WS! (and not a windows machine in sight.. I feel dirty but I like it :-) )



=== UPDATE ===

Ok after reading a lot of blogs and a few weeks later i've found a nicer way of doing it.

Instead of editing the xml you can choose to add a new webservice and select your webservice class (note screens below are not for the same project but are functionaly the same




Customization of WebApi


If you’ve used Asp MVC Web Api then you are most likely familiar with the notion of content negotiation, this is the process where the content returned in the response is dictated by the accepts header in the request. In sort if you request xml you get xml back, if you request json you get json back.

This is done by what we call Formatters. You can of course add your own formatters, e.g. let’s say you have an application that returns human resource details; you may request back a profile picture by hitting the same route with a different request header.


OOB Formatters

Lets take a working example, of what we get out of the box (OOB).

Create a new Web Api project

Add the following class


Modify the ValuesController as follows


We now should be able to run the project and see the following in the browser


So by default we get back an xml formatted response, of course we could request json, but what if we just don’t want xml?

Tweaking the config


Add the following line to your Global.asax.cs


Now add this new GlobalConfig static class as follows


Run your application again


Now we get json by default, yah!


But wait a second, what if I know that I have some javascript developers that want to use this content, wouldn’t be nicer to offer camel casing to these guys?


Run project again


e.g some simple jQuery