{"id":13734598,"url":"https://github.com/alexandre-spieser/AspNetCore.Identity.MongoDbCore","last_synced_at":"2025-05-08T10:32:47.919Z","repository":{"id":26279007,"uuid":"107821176","full_name":"alexandre-spieser/AspNetCore.Identity.MongoDbCore","owner":"alexandre-spieser","description":"A MongoDb UserStore and RoleStore adapter for Microsoft.AspNetCore.Identity 2.2. Allows you to use MongoDb instead of SQL server with Microsoft.AspNetCore.Identity 2.2. (not Identity 3.0)","archived":false,"fork":false,"pushed_at":"2024-11-26T23:22:40.000Z","size":1277,"stargazers_count":260,"open_issues_count":9,"forks_count":70,"subscribers_count":13,"default_branch":"master","last_synced_at":"2024-11-27T00:20:09.387Z","etag":null,"topics":["asp-net-core","aspnetcore","identity","mongodb","rolestore","store","userstore"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alexandre-spieser.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-10-21T22:44:20.000Z","updated_at":"2024-11-26T23:12:12.000Z","dependencies_parsed_at":"2024-11-15T03:30:54.168Z","dependency_job_id":"86a91b82-8b07-4c65-89d9-b70d7941da55","html_url":"https://github.com/alexandre-spieser/AspNetCore.Identity.MongoDbCore","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexandre-spieser%2FAspNetCore.Identity.MongoDbCore","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexandre-spieser%2FAspNetCore.Identity.MongoDbCore/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexandre-spieser%2FAspNetCore.Identity.MongoDbCore/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexandre-spieser%2FAspNetCore.Identity.MongoDbCore/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alexandre-spieser","download_url":"https://codeload.github.com/alexandre-spieser/AspNetCore.Identity.MongoDbCore/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253046011,"owners_count":21845825,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asp-net-core","aspnetcore","identity","mongodb","rolestore","store","userstore"],"created_at":"2024-08-03T03:00:57.885Z","updated_at":"2025-05-08T10:32:47.538Z","avatar_url":"https://github.com/alexandre-spieser.png","language":"C#","funding_links":["https://www.buymeacoffee.com/zeitquest"],"categories":["Libraries"],"sub_categories":["MongoDb"],"readme":"# AspNetCore.Identity.MongoDbCore\n\nA MongoDb UserStore and RoleStore adapter for Microsoft.AspNetCore.Identity 2.0 and 3.1.\nAllows you to use MongoDb instead of SQL server with Microsoft.AspNetCore.Identity 2.0 and 3.1.\n\nCovered 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.\n\nSupports both `netstandard2.1` and `netcoreapp3.1`.\n\nAvailable as a Nuget package : https://www.nuget.org/packages/AspNetCore.Identity.MongoDbCore/\n\n\tInstall-Package AspNetCore.Identity.MongoDbCore\n\n# Support This Project\n\nIf you have found this project helpful, either as a library that you use or as a learning tool, please consider buying Alex a coffee: \u003ca href=\"https://www.buymeacoffee.com/zeitquest\" target=\"_blank\"\u003e\u003cimg height=\"40px\" src=\"https://cdn.buymeacoffee.com/buttons/default-orange.png\" alt=\"Buy Me A Coffee\" style=\"max-height: 51px;width: 150px !important;\" \u003e\u003c/a\u003e\n\n\n# User and Role Entities\nYour user and role entities must inherit from `MongoIdentityUser\u003cTKey\u003e` and `MongoIdentityRole\u003cTKey\u003e` in a way similar to the `IdentityUser\u003cTKey\u003e` and the `IdentityRole\u003cTKey\u003e` in `Microsoft.AspNetCore.Identity`, where `TKey` is the type of the primary key of your document.\n\nHere is an example:\n\n```csharp\n\npublic class ApplicationUser : MongoIdentityUser\u003cGuid\u003e\n{\n\tpublic ApplicationUser() : base()\n\t{\n\t}\n\n\tpublic ApplicationUser(string userName, string email) : base(userName, email)\n\t{\n\t}\n}\n\npublic class ApplicationRole : MongoIdentityRole\u003cGuid\u003e\n{\n\tpublic ApplicationRole() : base()\n\t{\n\t}\n\n\tpublic ApplicationRole(string roleName) : base(roleName)\n\t{\n\t}\n}\t\n```\n#### Id Fields\nThe `Id` field is automatically set at instantiation, this also applies to users inheriting from `MongoIdentityUser\u003cint\u003e`, 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`, which both use the `Guid` type for primary keys, is recommended. MongoDB ObjectIds can optionally be used in lieu of GUIDs by passing a key type of `MongoDB.Bson.ObjectId`, e.g. `public class ApplicationUser : MongoIdentityUser\u003cObjectId\u003e`.\n\n#### Collection Names\nMongoDB collection names are set to the plural camel case version of the entity class name, e.g. `ApplicationUser` becomes `applicationUsers`. To override this behavior apply the `CollectionName` attribute from the `MongoDbGenericRepository` nuget package:\n```csharp\nusing MongoDbGenericRepository.Attributes;\n\nnamespace App.Entities\n{\n    // Name this collection Users\n    [CollectionName(\"Users\")]\n    public class ApplicationUser : MongoIdentityUser\u003cGuid\u003e\n    {\n\t...\n```\n# Configuration\nTo add the stores, you can use the `IdentityBuilder` extension like so:\n\n```csharp\nservices.AddIdentity\u003cApplicationUser, ApplicationRole\u003e()\n\t.AddMongoDbStores\u003cApplicationUser, ApplicationRole, Guid\u003e\n\t(\n\t\t\"mongodb://localhost:27017\",\n\t\t\"MongoDbTests\"\n\t)\n\t.AddDefaultTokenProviders();\n```\n\n\nIt is also possible to share a common `IMongoDbContext` across your services (requires https://www.nuget.org/packages/MongoDbGenericRepository/):\n\n```csharp\nvar mongoDbContext = new MongoDbContext(\"mongodb://localhost:27017\", \"MongoDbTests\");\nservices.AddIdentity\u003cApplicationUser, ApplicationRole\u003e()\n\t.AddMongoDbStores\u003cIMongoDbContext\u003e(mongoDbContext)\n\t.AddDefaultTokenProviders();\n// Use the mongoDbContext for other things.\n```\n\nYou can also use the more explicit type declaration:\n\n```csharp\nvar mongoDbContext = new MongoDbContext(\"mongodb://localhost:27017\", \"MongoDbTests\");\nservices.AddIdentity\u003cApplicationUser, ApplicationRole\u003e()\n\t.AddMongoDbStores\u003cApplicationUser, ApplicationRole, Guid\u003e(mongoDbContext)\n\t.AddDefaultTokenProviders();\n// Use the mongoDbContext for other things.\n```\n\nAlternatively 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\u003cIdentityOptions\u003e`).\n\nThe `MongoDbSettings` object is used to set MongoDb Settings using the `ConnectionString` and the `DatabaseName` properties.\n\nThe 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.\n\n```csharp\nvar mongoDbIdentityConfiguration = new MongoDbIdentityConfiguration\n{\n\tMongoDbSettings = new MongoDbSettings\n\t{\n\t\tConnectionString = \"mongodb://localhost:27017\",\n\t\tDatabaseName = \"MongoDbTests\"\n\t},\n\tIdentityOptionsAction = options =\u003e\n\t{\n\t\toptions.Password.RequireDigit = false;\n\t\toptions.Password.RequiredLength = 8;\n\t\toptions.Password.RequireNonAlphanumeric = false;\n\t\toptions.Password.RequireUppercase = false;\n\t\toptions.Password.RequireLowercase = false;\n\n\t\t// Lockout settings\n\t\toptions.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30);\n\t\toptions.Lockout.MaxFailedAccessAttempts = 10;\n\n\t\t// ApplicationUser settings\n\t\toptions.User.RequireUniqueEmail = true;\n\t\toptions.User.AllowedUserNameCharacters = \"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@.-_\";\n\t}\n};\nservices.ConfigureMongoDbIdentity\u003cApplicationUser, ApplicationRole, Guid\u003e(mongoDbIdentityConfiguration)\n        .AddDefaultTokenProviders();\n```\n\n# Running the tests\n\nTo run the tests, you need a local MongoDb server in default configuration (listening to `localhost:27017`).\nCreate a database named MongoDbTests for the tests to run.\n\n## Author\n**Alexandre Spieser**\n\n## License\nAspNetCore.Identity.MongoDbCore is under MIT license - http://www.opensource.org/licenses/mit-license.php\n\nThe MIT License (MIT)\n\nCopyright (c) 2016-2021 Alexandre Spieser\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n\n## Copyright\nCopyright © 2021\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexandre-spieser%2FAspNetCore.Identity.MongoDbCore","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexandre-spieser%2FAspNetCore.Identity.MongoDbCore","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexandre-spieser%2FAspNetCore.Identity.MongoDbCore/lists"}