notes from a passionate developer





This is a personal blog. The opinions expressed here represent my own and not those of my employer, nor current or previous. All content is published "as is", without warranty of any kind and I don't take any responsibility and can't be liable for any claims, damages or other liabilities that might be caused by the content.

Using MyCouch to store ASP.Net identity data in CouchDb or in the cloud using Cloudant

Recently I put out a simple NuGet package: MyCouch.AspNet.Identity, that lets you store ASP.Net identity data in CouchDb or in the cloud using Cloudant, instead of in a SQL Server using Entity framework. It’s about a three-five minute change and it is really easy. And to show you this, I have put together a small screencast as well as a small sample, which you can find in this article.


Note. This is so far a pre v1.0.0 product.

There is a simple sample in the repository, that makes use of a local CouchDb install. It requires:

You see this in Global.asax.

The sample is a basic ASP.Net MVC5 application, where Entity framework’s ASP.Net Identity provider has been replaced with:

install-package MyCouch.AspNet.Identity

To create the necessary views (secondary indexes), the user associated with the MyCouchClient that creates the views using the extension method Client.EnsureDesignDocsExists(), needs to have correct user-rights. It needs to be able to create views. Read more about this below.

You need a MyCouch Client. In the sample this is created in Global.asax:

public class MvcApplication : System.Web.HttpApplication
  protected async void Application_Start()

    using (var client = new MyCouchClient(CreateUri()))
      //Only needed once, and you can use a specific admin user to bootstrap this
      await client.EnsureDesignDocsExists();

  protected void Application_BeginRequest()
    HttpContext.Current.Items["MyCouchClient"] = new MyCouchClient(CreateUri());

  protected void Application_EndRequest()
    var client = HttpContext.Current.Items["MyCouchClient"] as IMyCouchClient;
    if (client != null)

  private static Uri CreateUri()
    return new MyCouchUriBuilder("http://localhost:5984")
      .SetBasicCredentials("demo", "p@ssword")

The EnsureDesignDocsExists method is only needed to be called once for the application life cycle. When the views has been created this call is redundant. And since it needs higher user-rights, this could be done while bootstrapping or seeding the application.

The Sample application in the repo was created with Entity framework as storage for the ASP.Net Identity models. In the AccountController, a MyCouchUserStore is instead injected into the ASP.Net Identity UserManager:

public class AccountController : Controller
  public AccountController()
    var client = (IMyCouchClient)System.Web.HttpContext.Current.Items["MyCouchClient"];
    UserManager = new UserManager(new MyCouchUserStore(client));


That’s it! To enable e.g. Google Auth. Just go to the Startup.Auth.cs file under App_Start, and uncomment:


To make use of roles, you can make use of the UserManager. A manual seed or creation would look something like this:

var mgr = new UserManager(new MyCouchUserStore(Client));
var usr = new ApplicationUser { UserName = "danieltest1" };
await mgr.CreateAsync(usr, "p@ssword");
await mgr.AddToRoleAsync(usr.Id, "Testers");

That’s it. Remember. It’s a pre v1.0.0 product.

If you want to know more about ASP.Net Identity, I’ve put together some few links below. It will be extended as I find more nice resources.



View Comments