{"id":20730166,"url":"https://github.com/mgernand/aspnetcore.identity.mongodb","last_synced_at":"2026-01-24T04:03:20.797Z","repository":{"id":148382844,"uuid":"617548744","full_name":"mgernand/AspNetCore.Identity.MongoDB","owner":"mgernand","description":"Provides MongoDB user store and role store implementations for ASP.NET Identity Core.","archived":true,"fork":false,"pushed_at":"2025-03-17T08:59:57.000Z","size":1137,"stargazers_count":10,"open_issues_count":0,"forks_count":4,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-05-03T02:05:42.903Z","etag":null,"topics":["asp-net-core","dotnet","dotnet7","dotnetcore","dotnetcore7","identity","mongodb","roles","users"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":false,"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/mgernand.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":"2023-03-22T16:05:38.000Z","updated_at":"2025-03-17T09:01:07.000Z","dependencies_parsed_at":null,"dependency_job_id":"7f469b5b-fc2c-49af-87bc-7a1903aee54e","html_url":"https://github.com/mgernand/AspNetCore.Identity.MongoDB","commit_stats":null,"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mgernand%2FAspNetCore.Identity.MongoDB","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mgernand%2FAspNetCore.Identity.MongoDB/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mgernand%2FAspNetCore.Identity.MongoDB/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mgernand%2FAspNetCore.Identity.MongoDB/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mgernand","download_url":"https://codeload.github.com/mgernand/AspNetCore.Identity.MongoDB/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253504508,"owners_count":21918820,"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","dotnet","dotnet7","dotnetcore","dotnetcore7","identity","mongodb","roles","users"],"created_at":"2024-11-17T05:10:12.467Z","updated_at":"2026-01-24T04:03:20.786Z","avatar_url":"https://github.com/mgernand.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"__I am leaving GitHub__\n\nPlease follow me on [Codeberg](https://codeberg.org/mgernand)!  \nYou can also find the sources of this repository there!\n\nOnwards! ✊\n\n# AspNetCore.Identity.MongoDB\n\nA libary that provides MongoDB UserStore and RoleStore implementations for ASP.NET Identity Core.\nIt allows you to use MongoDB with ASP.NET Core Identity.\n\n## Identity Entities\n\nIf you choose to use custom user and role entities be reminded that those entities must inherit from \n```MongoIdentityUser\u003cTKey\u003e``` and ```MongoIdentityRole\u003cTKey\u003e```, where ```TKey``` is the type of the\nID of the entity. The library supports either ```string``` or ```Guid``` type IDs. When ```string```\nis used, the MongoDB native ```ObjectId``` will be used as database ID type.\n\nThe following example shows custom user and role entities:\n\n```C#\npublic class ApplicationUser : MongoIdentityUser\u003cstring\u003e\n{\n\tpublic ApplicationUser()\n\t{\n\t}\n\n\tpublic ApplicationUser(string userName) \n\t\t: base(userName)\n\t{\n\t}\n\n\tpublic string FirstName { get; set; }\n\n\tpublic string LastName { get; set; }\n}\n\npublic class ApplicationRole : MongoIdentityRole\u003cstring\u003e\n{\n\tpublic ApplicationRole()\n\t{\n\t}\n\n\tpublic ApplicationRole(string roleName)\n\t\t: base(roleName)\n\t{\n\t}\n\n\tpublic string DisplayText { get; set; }\n}\n```\n\nUsing custom entity classes allows the developer to extend to store additional data besides the data needed\nby the Identity system.\n\nIf no custom enities are needed, one can use the default implementations using ```string``` IDs:\n\n- ```MongoIdentityUser```\n- ```MongoIdentityRole```\n\n## Usage\n\nTo use the MongoDB stores with ASP.NET Identity use the ```IdentityBuilder``` extension ```AddMongoDbStores```\nand configure the ```MongoDbContext``` using the ```AddMongoDbContext``` extension. \n\nThe stores support user/role and user-only configuration.\n\n### Configure user/role mode\n\n```C#\nbuilder.Services\n\t.AddAuthentication(IdentityConstants.ApplicationScheme)\n\t.AddIdentityCookies();\n\nbuilder.Services.AddMongoDbContext\u003cMongoDbContext\u003e(options =\u003e\n{\n\toptions.ConnectionString = \"mongodb://localhost:27017\";\n\toptions.DatabaseName = \"identity\";\n})\n.AddIdentityCore\u003cMongoIdentityUser\u003e()\n.AddRoles\u003cMongoIdentityRole\u003e()\n.AddDefaultTokenProviders()\n.AddMongoDbStores\u003cSampleContext\u003e();\n```\n\n### Configure user-only mode\n\n```C#\nbuilder.Services\n\t.AddAuthentication(IdentityConstants.ApplicationScheme)\n\t.AddIdentityCookies();\n\nbuilder.Services.AddMongoDbContext\u003cMongoDbContext\u003e(options =\u003e\n{\n\toptions.ConnectionString = \"mongodb://localhost:27017\";\n\toptions.DatabaseName = \"identity\";\n})\n.AddIdentityCore\u003cMongoIdentityUser\u003e()\n.AddDefaultTokenProviders()\n.AddMongoDbStores\u003cSampleContext\u003e();\n```\n\n## Using a custom context\n\nTo use a custom ```MongoDbContext``` just create a new class that inherits from \n```MongoDbContext```. Using this context one can change the used collection names.\nThe default names are ```AspNetUsers``` and ```AspNetRoles```. The sample application shows\nhow to change them into custom names.\n\n## Initialize the MongoDB driver\n\nAfter configuring the service of the system one has to initialize the MongoDB stores database\nusing the ```IServiceProvider``` extension ```InitializeMongoDbStores```. The configuration \nwill ensure the needed conventions are setup for the MongoDB driver and will ensure that the \ncollections are created with the needed indexes for the user and role entities.\n\n```C#\nawait app.Services.InitializeMongoDbStores();\n```\n\n## Data Protection support\n\nIf the Identity system was configured to opt-in to use the data protection API the MongoDB driver\nwill be automatically configured to protect/unprotect properties annotated with the ```[ProtectedPersonalData]```\nattribute by adding a specialized string serializer. You will still need to implement the \nneeded services and specialized user store that implements ```IProtectedUserStore\u003cTUser\u003e```.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmgernand%2Faspnetcore.identity.mongodb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmgernand%2Faspnetcore.identity.mongodb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmgernand%2Faspnetcore.identity.mongodb/lists"}