Thursday 26 March 2015

Simple.Data Asp.Net Identity Provider

ASP.Net Identity is Microsoft's latest implementation of a membership and identity service which will replace ASP.Net Membership, SimpleMembership and Universal Membership.

The out of the box implementation of ASP.Net Identity is, unsurprisingly, coupled to Entity Framework (EF) which means if you do want to use the new identity functionality but your app doesn’t use EF for data access you either end up having to include it in your project anyway or not use Identity.

Simple.Data

On a project I was part of towards the end of last year we ran into this exact problem, we were using Simple.Data for data access but wanted to use the new features of Identity.

One of the advantages of identity over previous membership implementations is that it is possible to provide a different data access layer allowing you to use the database you want but keep the rest of the functionality provided by identity e.g. password encryption, OAuth, etc

The advantage of using Simple.Data is that you aren’t limited to connecting to any one database as you simply include the relevant provider for the database you want to use (if it is supported).

The Provider

The result is a Simple.Data provider for ASP.Net Identity which should be able to dropped into an existing application using EF with little to no change.

You can install it from Nuget using the package manager console with the following command:

PM> Install-Package Simple.Data.AspNet.Identity

If you prefer to use the “Manage Nuget Packages” GUI it is easiest to search for Simple.Data:

image

If you want to just look at the code or feel like building it from code yourself the repo on GitHub can be found here.

Limitations

There are a few limitations with this implementation that its worth knowing about:

  • Due to ASP.Net Identity being designed around generics you must use concrete classes with it, you cannot use dynamic.  To help the provider comes with 2 classes IdentityUser and IdentityRole classes for you to use.
  • Database tables – currently you need to use database tables that match the IdentityUser and IdentityRole classes although they don’t have to be named the same.  The Nuget package includes a script for Sql Server to create these tables.
  • Relies on referential integrity – when deleting data ASP.Net Provider does delete any data that may be effected, instead it relies on db referential integrity to clean up linked records.
  • Ids stored as strings – although ids for users, roles, etc are generated as Guids they are stored as strings, this mimics the standard EF model.
  • No IQueryable support – Simple.Data doesn’t support IQueryable this means that the Users property on UserStore and Roles property on RoleStore will throw not implemented exceptions.  If you need to query Users and Roles you need to do that yourself
  • Only works with Simple.Data v1 – a version is coming for Simple.Data v2 (which supports async await) but this is still a work in progress.
  • Db Connection – the provider supports using Simple.Data default connection or a named connection, it currently doesn’t support taking a connection string directly.

Although you need to use the identity classes in the provider you can inherit from them just like the EF code.

Samples

To try and make it as easy as possible to get started with this provider I have built 2 sample applications based off of the normal MVC project template:

  1. MVC 5 Standard Sample – this is the standard MVC project template the only change is that it uses Simple.Data for data access.
  2. MVC 5 Sample – this sample is an extension of the standard sample as it includes using roles with identity to be able to control access to pages of your web application

The easiest way to work with these samples is:

  1. Create new ASP.Net Web application
  2. Select the Empty project template
  3. Once the project has been created add the appropriate Nuget package

If you want the standard sample then at the package manager console type:

PM > Install-Package Simple.Data.AspNet.Identity.MVC5.Standard.Sample

Or if you want the sample that includes roles:

PM> Install-Package Simple.Data.AspNet.Identity.MVC5.Sample

When you install these packages you will have all the necessary classes and references added to your project and it will create a database in localDb so you should be able to build and run the code to start working with it immediately.

Both sample projects are in one repo which you can find here if you wish to play with the code that creates the samples although installing the Nuget package is by far the easiest way to get working with the code.

The future

This version is 1.0.0, it is stable and should work without problems (taking limitations mentioned into account).

Going forward there are several improvements that I intend to make:

  • Simple.Data v2 – complete work on this implementation
  • Support connection strings – to match normal Simple.Data connection options add the ability to be passed a connection string.
  • Support different id variable types – normal ASP.Net Identity allows for different variable types for ids via more generics, I’m looking to add this in.
  • Automated builds – looking to use AppVeyor to build and test the code
  • Automate Nuget publishing – once AppVeyor is building the code look to automate generation and publishing of the Nuget packages
  • More samples – add samples showing use of the provider in different ways e.g. not tied to Owin context

Summary

This provider should make it easy for people that wish to use ASP.Net Identity with Simple.Data right now.

With the planned future improvements the additional functionality will bring it up to parity with the full EF model.


No comments:

Post a Comment