Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mgernand/aspnetcore.identity.mongodb
A libary that provides MongoDB UserStore and RoleStore implementations for ASP.NET Identity Core.
https://github.com/mgernand/aspnetcore.identity.mongodb
asp-net-core dotnet dotnet7 dotnetcore dotnetcore7 identity mongodb roles users
Last synced: 1 day ago
JSON representation
A libary that provides MongoDB UserStore and RoleStore implementations for ASP.NET Identity Core.
- Host: GitHub
- URL: https://github.com/mgernand/aspnetcore.identity.mongodb
- Owner: mgernand
- License: mit
- Created: 2023-03-22T16:05:38.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-11-14T12:43:58.000Z (4 days ago)
- Last Synced: 2024-11-14T13:30:58.611Z (4 days ago)
- Topics: asp-net-core, dotnet, dotnet7, dotnetcore, dotnetcore7, identity, mongodb, roles, users
- Language: C#
- Homepage:
- Size: 1.07 MB
- Stars: 10
- Watchers: 3
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# AspNetCore.Identity.MongoDB
A libary that provides MongoDB UserStore and RoleStore implementations for ASP.NET Identity Core.
It allows you to use MongoDB with ASP.NET Core Identity.## Identity Entities
If you choose to use custom user and role entities be reminded that those entities must inherit from
```MongoIdentityUser``` and ```MongoIdentityRole```, where ```TKey``` is the type of the
ID of the entity. The library supports either ```string``` or ```Guid``` type IDs. When ```string```
is used, the MongoDB native ```ObjectId``` will be used as database ID type.The following example shows custom user and role entities:
```C#
public class ApplicationUser : MongoIdentityUser
{
public ApplicationUser()
{
}public ApplicationUser(string userName)
: base(userName)
{
}public string FirstName { get; set; }
public string LastName { get; set; }
}public class ApplicationRole : MongoIdentityRole
{
public ApplicationRole()
{
}public ApplicationRole(string roleName)
: base(roleName)
{
}public string DisplayText { get; set; }
}
```Using custom entity classes allows the developer to extend to store additional data besides the data needed
by the Identity system.If no custom enities are needed, one can use the default implementations using ```string``` IDs:
- ```MongoIdentityUser```
- ```MongoIdentityRole```## Usage
To use the MongoDB stores with ASP.NET Identity use the ```IdentityBuilder``` extension ```AddMongoDbStores```
and configure the ```MongoDbContext``` using the ```AddMongoDbContext``` extension.The stores support user/role and user-only configuration.
### Configure user/role mode
```C#
builder.Services
.AddAuthentication(IdentityConstants.ApplicationScheme)
.AddIdentityCookies();builder.Services.AddMongoDbContext(options =>
{
options.ConnectionString = "mongodb://localhost:27017";
options.DatabaseName = "identity";
})
.AddIdentityCore()
.AddRoles()
.AddDefaultTokenProviders()
.AddMongoDbStores();
```### Configure user-only mode
```C#
builder.Services
.AddAuthentication(IdentityConstants.ApplicationScheme)
.AddIdentityCookies();builder.Services.AddMongoDbContext(options =>
{
options.ConnectionString = "mongodb://localhost:27017";
options.DatabaseName = "identity";
})
.AddIdentityCore()
.AddDefaultTokenProviders()
.AddMongoDbStores();
```## Using a custom context
To use a custom ```MongoDbContext``` just create a new class that inherits from
```MongoDbContext```. Using this context one can change the used collection names.
The default names are ```AspNetUsers``` and ```AspNetRoles```. The sample application shows
how to change them into custom names.## Initialize the MongoDB driver
After configuring the service of the system one has to initialize the MongoDB stores database
using the ```IServiceProvider``` extension ```InitializeMongoDbStores```. The configuration
will ensure the needed conventions are setup for the MongoDB driver and will ensure that the
collections are created with the needed indexes for the user and role entities.```C#
await app.Services.InitializeMongoDbStores();
```## Data Protection support
If the Identity system was configured to opt-in to use the data protection API the MongoDB driver
will be automatically configured to protect/unprotect properties annotated with the ```[ProtectedPersonalData]```
attribute by adding a specialized string serializer. You will still need to implement the
needed services and specialized user store that implements ```IProtectedUserStore```.