2

For my REST Api I want to use the OpenIddict authentication scheme. As database I am using MongoDb. I have installed all necessary dependencies. All dependencies have the newest version.

In my Startup.cs I want to register OpenIddict now. In the first step make this (like in the documentaion)

services.AddDbContext<UserDbContext>(options =>
{
    options.UseOpenIddict<ObjectId>();
});

Right on options.UseOpenIddict<ObjectId>(); I get the following error:

'DbContextOptionsBuilder' does not contain a definition for 'UseOpenIddict' and no accessible extension method 'UseOpenIddict' accepting a first argument of type 'DbContextOptionsBuilder' could be found(are you missing a using directive or an assembly reference?)

It's the CS1061 error.

I am using all directives. I have googled a lot. The only thing I found was that you need to install the required packages, but I installed them. (In the solution files of the tutorial I am following are the exact same ones)

Does anyone know how to solve this?

2
  • 2
    If you use MongoDB, why do you call services.AddDbContext(), which is an Entity Framework method? OpenIddict natively supports MongoDB: nuget.org/packages/OpenIddict.MongoDb Commented Jul 12, 2020 at 1:00
  • 1
    @KévinChalet thank you. This helped a lot. I added my solution to my answer now Commented Jul 12, 2020 at 13:08

1 Answer 1

7

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) { }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I'm assuming you're using this NuGet package for AddIdentityMongoDbProvider?
@MarkMalabanan yes I am

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.