This commit is contained in:
alexandre-spieser
2017-10-22 01:24:36 +00:00
+19 -15
View File
@@ -1,26 +1,16 @@
# AspNetCore.Identity.MongoDbCore # AspNetCore.Identity.MongoDbCore
A MongoDb UserStore and RoleStore adapter for Microsoft.AspNetCore.Identity 2.0. A MongoDb UserStore and RoleStore adapter for Microsoft.AspNetCore.Identity 2.0.
Allows you to use MongoDb instead of SQL server with Microsoft.AspNetCore.Identity 2.0. Allows you to use MongoDb instead of SQL server with Microsoft.AspNetCore.Identity 2.0.
Covered by 737 integration tests and unit tests from the modified [Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test](https://github.com/aspnet/Identity/tree/b865d5878623077eeb715e600d75fa9c24dbb5a1/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test) test suite. Covered by 737 integration tests and unit tests from the modified [Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test](https://github.com/aspnet/Identity/tree/b865d5878623077eeb715e600d75fa9c24dbb5a1/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test) test suite.
# Usage examples # Usage examples
Your user and role entities must inherit from MongoIdentityUser<Guid> and MongoIdentityRole<TKey> in a way similar to the IdentityUser<TKey> and the IdentityRole<TKey> in Microsoft.AspNetCore.Identity. Your user and role entities must inherit from MongoIdentityUser<Guid> and MongoIdentityRole<TKey> in a way similar to the IdentityUser<TKey> and the IdentityRole<TKey> in Microsoft.AspNetCore.Identity.
Here is an example: Here is an example:
```csharp ```csharp
public class ApplicationRole : MongoIdentityRole<Guid>
{
public ApplicationRole() : base()
{
}
public ApplicationRole(string roleName) : base(roleName)
{
}
}
public class ApplicationUser : MongoIdentityUser<Guid> public class ApplicationUser : MongoIdentityUser<Guid>
{ {
public ApplicationUser() : base() public ApplicationUser() : base()
@@ -31,8 +21,22 @@ Here is an example:
{ {
} }
} }
public class ApplicationRole : MongoIdentityRole<Guid>
{
public ApplicationRole() : base()
{
}
public ApplicationRole(string roleName) : base(roleName)
{
}
}
``` ```
The `Id` field is automatically set at instanciation, this also applies to users inheriting from `MongoIdentityUser<int>`, where a random integer is assigned to the `Id`. It is however not advised to rely on such random mechanism to set the primary key of your document. Using documents inheriting from `MongoIdentityRole<Guid>` is recommended.
The configuration is done by populating a `MongoDbIdentityConfiguration` object, which can have an `IdentityOptionsAction` property set to an action you want to perform against the `IdentityOptions` (`Action<IdentityOptions>`). The configuration is done by populating a `MongoDbIdentityConfiguration` object, which can have an `IdentityOptionsAction` property set to an action you want to perform against the `IdentityOptions` (`Action<IdentityOptions>`).
The `MongoDbSettings` object is used to set MongoDb Settings using the `ConnectionString` and the `DatabaseName` properties. The `MongoDbSettings` object is used to set MongoDb Settings using the `ConnectionString` and the `DatabaseName` properties.
The MongoDb connection is managed using the [mongodb-generic-repository](https://github.com/alexandre-spieser/mongodb-generic-repository), where a repository inheriting `IBaseMongoRepository` is registered as a singleton. Look at the [ServiceCollectionExtension.cs](https://github.com/alexandre-spieser/AspNetCore.Identity.MongoDbCore/blob/master/src/Extensions/ServiceCollectionExtension.cs) file for more details. The MongoDb connection is managed using the [mongodb-generic-repository](https://github.com/alexandre-spieser/mongodb-generic-repository), where a repository inheriting `IBaseMongoRepository` is registered as a singleton. Look at the [ServiceCollectionExtension.cs](https://github.com/alexandre-spieser/AspNetCore.Identity.MongoDbCore/blob/master/src/Extensions/ServiceCollectionExtension.cs) file for more details.
@@ -56,6 +60,7 @@ The MongoDb connection is managed using the [mongodb-generic-repository](https:/
// add a global config object // add a global config object
services.AddSingleton(Configuration); services.AddSingleton(Configuration);
// get the MongoDb settings from the Configuration object.
var settings = Configuration.GetSection(nameof(MongoDbSettings)).Get<MongoDbSettings>(); var settings = Configuration.GetSection(nameof(MongoDbSettings)).Get<MongoDbSettings>();
var mongoDbIdentityConfiguration = new MongoDbIdentityConfiguration var mongoDbIdentityConfiguration = new MongoDbIdentityConfiguration
{ {
@@ -72,7 +77,6 @@ The MongoDb connection is managed using the [mongodb-generic-repository](https:/
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30); options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30);
options.Lockout.MaxFailedAccessAttempts = 10; options.Lockout.MaxFailedAccessAttempts = 10;
// ApplicationUser settings // ApplicationUser settings
options.User.RequireUniqueEmail = true; options.User.RequireUniqueEmail = true;
options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@.-_"; options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@.-_";
@@ -88,12 +92,12 @@ The MongoDb connection is managed using the [mongodb-generic-repository](https:/
## Donations ## Donations
Feeling like my work is worth a coffee? Feeling like my work is worth a coffee?
Donations are welcome and will go towards further development of this project as well as other MongoDb related projects. Use the wallet address below to donate. Donations are welcome and will go towards further development of this project as well as other MongoDb related projects. Use the wallet address below to donate.
BTC Donations: 1Qc5ZpNA7g66KEEMcz7MXxwNyyoRyKJJZ BTC Donations: 1FDMWqSK8SHXDGKKp7gyZc4rknynWJ7qbj
*Thank you for your support and generosity!* *Thank you for your support and generosity!*
## License ## License
mongodb-generic-repository is under MIT license - http://www.opensource.org/licenses/mit-license.php AspNetCore.Identity.MongoDbCore is under MIT license - http://www.opensource.org/licenses/mit-license.php
The MIT License (MIT) The MIT License (MIT)