-
Notifications
You must be signed in to change notification settings - Fork 10
Home
Note. The v1.0.0 release is not backwards compatible with previous releases. The IdentityUser now uses Email as the value for both UserName and Id so the value stored in the database for _id will now contain the email.
There's a blog and screencast of pre v1.0.0 here: http://danielwertheim.se/2014/01/24/using-mycouch-to-store-asp-net-identity-data-in-couchdb-or-in-the-cloud-using-cloudant/
There is a simple sample in the repository, that makes use of a local CouchDB install. 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
You need a MyCouch Client. In the sample this is simply created in Global.asax, but this is just for demo purposes. You will probably make use of an IoC-container instead.
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);
//Demo bootstrap of ensuring database existance
//and view existance.
//NOTE! Only needed once per DB and should
//use some sort of bootstrap account.
using (var client = CreateClient())
{
//Idem potent
await client.Database.PutAsync();
//Create secodnary indexes a.k.a views
await client.EnsureAspNetIdentityDesignDocsExists();
//If you want to force a restore if the original shipped view
//await client.EnsureCleanAspNetIdentityDesignDocsExists();
}
}To create the necessary views (secondary indexes), the user associated with the MyCouchClient that creates the views using the extension method Client.EnsureAspNetIdentityDesignDocsExists(), needs to have correct user-rights. It needs to be able to create views. There's also a Client.EnsureCleanAspNetIdentityDesignDocsExists() that overwrites any existing view.
The EnsureAspNetIdentityDesignDocsExists() 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 initially created with Entity framework as storage for the ASP.Net Identity models.
In Startup, we register an IMyCouchClient for a request:
public partial class Startup
{
//...cleared...cleared
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context and user manager
// to use a single instance per request
app.CreatePerOwinContext(MvcApplication.CreateClient);
//...cleared...cleared
}
//...cleared...cleared
}In Startup.Auth.cs is also where you in the scaffolded sample from Microsoft, enable third party authentication like Google, Microsoft, Twitter etc. Just get a ClientId and Secret for each individual provider and uncomment e.g.:
app.UseMicrosoftAccountAuthentication(
clientId: "clientid-from-eg-web-config",
clientSecret: "secret-from-eg-web-config");This is one of the places where you can configure the behavior of the ASP.Net Identity "framework". You do need to import the MyCouch.AspNet.Identity namespace and let the ApplicationManager make use of the MyCouchUserStore instead.
public class ApplicationUserManager : UserManager<ApplicationUser>
{
//...cleared...
public static ApplicationUserManager Create(
IdentityFactoryOptions<ApplicationUserManager> options,
IOwinContext context)
{
var client = context.Get<IMyCouchClient>();
var userStore = new MyCouchUserStore<ApplicationUser>(client);
var manager = new ApplicationUserManager(userStore);
//...cleared...cleared
}
//...cleared...cleared
}To make use of roles with e.g. [Authorize(Roles = "SuperHeroes")] (See HomeController.About). To assign roles to a user, you can make use of the UserManager. A manual seed or creation would look something like this:
var userStore = new MyCouchUserStore<ApplicationUser>(Client);
var mgr = new UserManager<ApplicationUser>(userStore);
var usr = new ApplicationUser { UserName = "daniel@foo.bar"};
await mgr.CreateAsync(usr, "p@ssword");
await mgr.AddToRoleAsync(usr.Id, "Testers");There's a simple view and model for assigning roles to the logged in user. See ~/account/assignrole.