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.
- 00:40 – Create an ASP.Net MVC5 app and try it out
- 01:40 – Remove Entity framework
- 02:34 – Use MyCouch against CouchDb
- 05:12 – Enable Google authentication
- 05:44 – Take it to the cloud with Cloudant
- 06:50 – Make use of roles
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:
demo
with 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 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.
- Asp.Net Identity at Asp.Net
- K.Scott Allen (blog)
- The Identity and Security module at Pluralsight by K.Scott Allen
Cheers,
//Daniel