Initial Setup - lkubis/AspNetCore.Identity.Cassandra GitHub Wiki
For .NET Core 2.2 and earlier versions. If you're targeting your application to .NET Core 3.1 and higher, please go to Initial Setup v2.3.0
Run the following command from the package manager console to install Cassandra identity provider.
Install-Package AspNetCore.Identity.Cassandra
[Table("users", Keyspace = "identity")]
public class ApplicationUser : CassandraIdentityUser
{
public ApplicationUser()
: base(Guid.NewGuid())
{
}
}
[Table("roles", Keyspace = "identity")]
public class ApplicationRole : CassandraIdentityRole
{
public ApplicationRole()
: base(Guid.NewGuid())
{
}
}
In Cassandra
root there are information about keyspace and replication options
ContactPoints
contains collection of IP addresses of all available nodes
Query
is transformed to CqlQueryOptions
{
"Cassandra": {
"ContactPoints": [
"127.0.0.1"
],
"Credentials": {
"UserName": "Cassandra",
"Password": "Cassandra"
},
"KeyspaceName": "identity",
"Replication": {
"class": "NetworkTopologyStrategy",
"datacenter1": "1"
},
"Query": {
"ConsistencyLevel": "One",
"TracingEnabled": true,
"PageSize": 25
}
}
}
public void ConfigureServices(IServiceCollection services)
{
// CassandraOptions configuration
services.Configure<CassandraOptions>(Configuration.GetSection("Cassandra"));
// Cassandra ISession initialization
services.AddCassandraSession<Cassandra.ISession>(() =>
{
var contactPoints = Configuration
.GetSection("Cassandra:ContactPoints")
.GetChildren()
.Select(x => x.Value);
var cluster = Cassandra.Cluster.Builder()
.AddContactPoints(contactPoints)
.WithCredentials(
Configuration.GetValue<string>("Cassandra:Credentials:UserName"),
Configuration.GetValue<string>("Cassandra:Credentials:Password"))
.Build();
var session = cluster.Connect();
return session;
});
// Added custom Cassandra stores
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddCassandraErrorDescriber<CassandraErrorDescriber>()
.UseCassandraStores<Cassandra.ISession>()
.AddDefaultTokenProviders();
// Other code omitted
}
Initialize Cassandra DB by calling InitializeIdentityDb<ApplicationUser, ApplicationRole>() method on IWebHost interface.
public static class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build()
.InitializeIdentityDb<ApplicationUser, ApplicationRole>();
}