The Problem was with a package that overrode the UseOpenIddict method. I have uninstalled the package an rewrote some code, since the package wasn't that necessary. This is the package that isn't compatible.
Update
Thank you Kévin Chalet for this comment.
I rewrote my identity configuration to
services.AddIdentityMongoDbProvider<UserEntity, UserRoleEntity>(mongo =>
{
mongo.ConnectionString = _databaseUri;
});
This works now perfectly for me.
Update 2
I have googled some more and I haven't found any solutions on how to properly implement OpenIddict and MongoDb. For someone who is just starting the following might help. My OpenIddict / authentication / authorization runs fine with the following configuration:
Startup.cs
ConfigureServices:
services.AddIdentityMongoDbProvider<UserEntity, UserRoleEntity>(mongo = >{
mongo.ConnectionString = _databaseUri;
});
services.Configure<IdentityOptions>(options = >{
options.ClaimsIdentity.UserNameClaimType = OpenIdConnectConstants.Claims.Name;
options.ClaimsIdentity.UserIdClaimType = OpenIdConnectConstants.Claims.Subject;
options.ClaimsIdentity.RoleClaimType = OpenIdConnectConstants.Claims.Role;
});
services.AddAuthentication(options = >{
options.DefaultScheme = OpenIddictValidationDefaults.AuthenticationScheme;
});
services
.AddOpenIddict()
.AddCore(options = >{
options.UseMongoDb()
.UseDatabase(new MongoClient(_databaseUri)
.GetDatabase(_database));
}).AddServer(options = >{
options.SetAccessTokenLifetime(TimeSpan.FromDays(5));
options.UseMvc();
options.EnableTokenEndpoint("/api/token");
options.EnableUserinfoEndpoint("/api/userinfo");
options.AllowPasswordFlow()
.AllowRefreshTokenFlow();
options.AcceptAnonymousClients();
}).AddValidation();
services.AddAuthorization(options = >{
options.DefaultPolicy = new AuthorizationPolicyBuilder()
.AddAuthenticationSchemes(OpenIddictValidationDefaults.AuthenticationScheme)
.RequireAuthenticatedUser()
.Build();
});
Configure:
app.UseAuthentication();
app.UseCors("AllowBrowserApp");
app.UseRouting();
app.UseAuthorization();
Note: It is important where you register the authentication and authorization. The authentication comes before app.UseRouting() and the authorization after that. Otherwise it won't work. If you use Visual Studio it will show you.
UserEntity.cs
public class UserEntity : MongoUser
{
public string Firstname { get; set; }
public string Lastname { get; set; }
}
You can add more properties if you want.
UserRoleEntity.cs
public class UserRoleEntity : MongoRole
{
public UserRoleEntity() : base() { }
public UserRoleEntity(string roleName) : base(roleName) { }
}
services.AddDbContext(), which is an Entity Framework method? OpenIddict natively supports MongoDB: nuget.org/packages/OpenIddict.MongoDb