The simple and easy Identity provider for RavenDB and ASP.NET Core. Use Raven to store your users and logins.
Important: Upgrading from a previous version of RavenDB.Identity? See Updating From Old Version for steps to migrate to the latest RavenDB.Identity.
- Add an AppUser class that derives from Raven.Identity.IdentityUser:
public class AppUser : Raven.Identity.IdentityUser
{
/// <summary>
/// A user's full name.
/// </summary>
public string FullName { get; set; }
}
- In appsettings.json, configure your connection to Raven:
"RavenSettings": {
"Urls": [
"http://live-test.ravendb.net"
],
"DatabaseName": "Raven.Identity.Sample",
"CertFilePath": "",
"CertPassword": ""
},
- In Startup.cs, wire it all up:
public void ConfigureServices(IServiceCollection services)
{
// Grab our RavenSettings object from appsettings.json.
services.Configure<RavenSettings>(Configuration.GetSection("RavenSettings"));
...
// Add RavenDB and identity.
services
.AddRavenDbDocStore() // Create an IDocumentStore singleton from the RavenSettings.
.AddRavenDbAsyncSession() // Create a RavenDB IAsyncDocumentSession for each request. You're responsible for calling .SaveChanges after each request.
.AddRavenDbIdentity<AppUser>(); // Use Raven to manage users and roles.
...
}
- In your controller actions, call .SaveChangesAsync() when you're done making changes. Typically this is done via a RavenController base class for MVC/WebAPI projects or via an action filter. See our sample RavenSaveChangesAsyncFilter.cs.
To modify RavenDB conventions, you can use the services.AddRavenDbDocStore(options)
overload:
services.AddRavenDbDocStore(options =>
{
options.BeforeInitializeDocStore = docStore => docStore.Conventions.IdentityPartsSeparator = "-";
})
Using an old version of RavenDB.Identity and want to upgrade to the latest? You need to call the MigrateToV6
method:
// Update our existing users to the latest RavenDB.Identity v6.
// This is necessary only if you stored users with a previous version of RavenDB.Identity.
// Failure to call this method will result in existing users not being able to login.
// This method can take several minutes if you have thousands of users.
UserStore<AppUser>.MigrateToV6(docStore);
This upgrade step is necessary because we updated RavenDB.Identity to use RavenDB's cluster-safe compare/exchange to enforce user name/email uniqueness.
Previous versions of RavenDB.Identity had relied on IdentityUserByUserName
IDs to enforce uniqueness, but this isn't guaranteed to work in a cluster. Calling MigrateToV6 will create compare/exchange values in Raven for each email address, and will remove the now-obsolete IdentityUserByUserNames collection.
Need help? Checkout the sample app to see it all in action.
See our sister project for a RavenDB Identity Provider for the full .NET Framework.