danielwertheim

danielwertheim


notes from a passionate developer

Developer that lives by the mantra "code is meant to be shared".

Share


Tags


Disclaimer

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

Daniel WertheimDaniel Wertheim

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.

Sample

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()
  {
    AreaRegistration.RegisterAllAreas();
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);

    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)
      client.Dispose();
  }

  private static Uri CreateUri()
  {
    return new MyCouchUriBuilder("http://localhost:5984")
      .SetDbName("aspnet-identity")
      .SetBasicCredentials("demo", "p@ssword")
      .Build();
  }
}

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:

[Authorize]
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:

app.UseGoogleAuthentication();  

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.

Cheers,

//Daniel

Developer that lives by the mantra "code is meant to be shared".

Comments