From 43b3d8efb5f92ef8708e20cab31787a1eae0ccba Mon Sep 17 00:00:00 2001 From: Alexandre SPIESER Date: Sun, 29 Oct 2017 18:23:59 +0000 Subject: [PATCH] Update README.md --- README.md | 132 +++++++++++++++++++++++------------------------------- 1 file changed, 56 insertions(+), 76 deletions(-) diff --git a/README.md b/README.md index 3f62cee..7ddc3b1 100644 --- a/README.md +++ b/README.md @@ -43,90 +43,70 @@ Here is an example: ``` The `Id` field is automatically set at instantiation, this also applies to users inheriting from `MongoIdentityUser`, 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` and `MongoIdentityUser` 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`). +To add the stores, you can use the `IdentityBuilder` extension like so: + +```csharp +services.AddIdentity() + .AddMongoDbStores + ( + "mongodb://localhost:27017", + "MongoDbTests" + ) + .AddDefaultTokenProviders(); +``` + +It is also possible to share a common `IMongoDbContext` across your services: + +```csharp +var mongoDbContext = new MongoDbContext("mongodb://localhost:27017", "MongoDbTests"); +services.AddIdentity() + .AddMongoDbStores(mongoDbContext) + .AddDefaultTokenProviders(); +// Use the mongoDbContext for other things. +``` + +You can also use the more explicit type declaration: + +```csharp +var mongoDbContext = new MongoDbContext("mongodb://localhost:27017", "MongoDbTests"); +services.AddIdentity() + .AddMongoDbStores(mongoDbContext) + .AddDefaultTokenProviders(); +// Use the mongoDbContext for other things. +``` + +Alternatively a full configuration can be done by populating a `MongoDbIdentityConfiguration` object, which can have an `IdentityOptionsAction` property set to an action you want to perform against the `IdentityOptions` (`Action`). 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. ```csharp - /// - /// This method gets called by the runtime. Use this method to add services to the container. - /// - /// - public void ConfigureServices(IServiceCollection services) - { - var builder = new ConfigurationBuilder() - .SetBasePath(_hostingEnvironment.ContentRootPath) - .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) - .AddJsonFile($"appsettings.{_hostingEnvironment.EnvironmentName}.json", optional: true); +var mongoDbIdentityConfiguration = new MongoDbIdentityConfiguration +{ + MongoDbSettings = new MongoDbSettings + { + ConnectionString = "mongodb://localhost:27017", + DatabaseName = "MongoDbTests" + }, + IdentityOptionsAction = options => + { + options.Password.RequireDigit = false; + options.Password.RequiredLength = 8; + options.Password.RequireNonAlphanumeric = false; + options.Password.RequireUppercase = false; + options.Password.RequireLowercase = false; + // Lockout settings + options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30); + options.Lockout.MaxFailedAccessAttempts = 10; - Configuration = builder.Build(); - - services.AddOptions(); - - // add a global config object - services.AddSingleton(Configuration); - // get the MongoDb settings from the Configuration object. - var settings = Configuration.GetSection(nameof(MongoDbSettings)).Get(); - var mongoDbIdentityConfiguration = new MongoDbIdentityConfiguration - { - MongoDbSettings = settings, - IdentityOptionsAction = options => - { - options.Password.RequireDigit = false; - options.Password.RequiredLength = 8; - options.Password.RequireNonAlphanumeric = false; - options.Password.RequireUppercase = false; - options.Password.RequireLowercase = false; - - // Lockout settings - options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30); - options.Lockout.MaxFailedAccessAttempts = 10; - - // ApplicationUser settings - options.User.RequireUniqueEmail = true; - options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@.-_"; - } - }; - services.ConfigureMongoDbIdentity(mongoDbIdentityConfiguration); - } -``` - -Alternatively you can use the `IdentityBuilder` extensions like so: - -```csharp - var settings = Configuration.GetSection(nameof(MongoDbSettings)).Get(); - services.AddIdentity() - .AddMongoDbStores - ( - settings.ConnectionString, - settings.DatabaseName - ) - .AddDefaultTokenProviders(); -``` - -It is also possible to share a common `IMongoDbContext` across your services: - -```csharp - var settings = Configuration.GetSection(nameof(MongoDbSettings)).Get(); - var mongoDbContext = new MongoDbContext(settings.ConnectionString, settings.DatabaseName); - services.AddIdentity() - .AddMongoDbStores(mongoDbContext) - .AddDefaultTokenProviders(); - // Use the mongoDbContext for other things. -``` - -You can also use the more explicit type declaration: - -```csharp - var settings = Configuration.GetSection(nameof(MongoDbSettings)).Get(); - var mongoDbContext = new MongoDbContext(settings.ConnectionString, settings.DatabaseName); - services.AddIdentity() - .AddMongoDbStores(mongoDbContext) - .AddDefaultTokenProviders(); - // Use the mongoDbContext for other things. + // ApplicationUser settings + options.User.RequireUniqueEmail = true; + options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@.-_"; + } +}; +services.ConfigureMongoDbIdentity(mongoDbIdentityConfiguration); ``` # Running the tests