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:

  • database: aspnet-identity
  • an user named: demowith the password: p@ssword.

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 lifecycle. When the views has been created this call is redundant. And since it needs higher user-rights, this could be done while bootstraping 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

Category:
CouchDb & Cloudant, Development, MyCouch, NoSQL
Tags:
, , , , ,

Join the conversation! 5 Comments

  1. […] Using MyCouch to store ASP.NET identity data in CouchDB or in the cloud using Cloudant – An introduction to the ASP.NET Identity provider for CouchDB and Cloudant created by Daniel Wertheim. For more information have a look in the GitHub section at the end of this article. […]

    Reply
  2. How to do the same in asp.net webforms?. Is there any advantages in using couchdb and What is the acutal purpose of MyCouch.AspNet.Identity. Why should I prefer it over normal sql server engine?

    Reply
  3. Can I use the MyCouch.AspNet.Identity as an alternative to asp.net sql membership. Or is just another oath implmentation?

    Reply
  4. Can I achieve the same result when using asp.net webforms?. Could you provide a working sample in asp.net webforms?

    Reply
    • - Do not know about webforms and the Identity model. Check the link to the ASP.Net site for info.

      – The purpose is to allow you to store the identity data in CouchDb or Cloudant

      – Advantages of CouchDb over SQL server? Well, e.g. you have CouchDb and its FREE and you don’t have SQL server which is NOT FREE. You have a more flexible (per entity) model in Couch. It’s like NoSQL vs RDBMS. Lots of stuff about it out there.

      – Check the links provided and you can check up more about the new identity model (replaces membership model etc) that is used.

      Cheers,

      //Daniel

      Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: