.NET Core 使用 MongoDb 配置 OpenIddict

.NET Core configure OpenIddict with MongoDb

对于我的 REST Api 我想使用 OpenIddict 身份验证方案。作为数据库,我使用 MongoDb。我已经安装了所有必要的依赖项。所有依赖项都有最新版本。

在我的 Startup.cs 中,我现在想注册 OpenIddict。在第一步中做这个(就像在文档中一样)

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

就在 options.UseOpenIddict<ObjectId>(); 我收到以下错误:

'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?)

这是 CS1061 错误。

我正在使用所有指令。我用谷歌搜索了很多。我唯一发现的是你需要安装所需的包,但我安装了它们。 (我下面的教程的解决方案文件是完全一样的)

有人知道怎么解决吗?

问题出在覆盖 UseOpenIddict 方法的包中。我已经卸载了这个包并重写了一些代码,因为这个包不是那么必要。 This 是不兼容的包。

更新

谢谢Kévin Chalet for

我将身份配置重写为

services.AddIdentityMongoDbProvider<UserEntity, UserRoleEntity>(mongo =>
{
    mongo.ConnectionString = _databaseUri;
});

现在这对我来说非常有用。

更新 2

我又用谷歌搜索了一些,但没有找到任何关于如何正确实施 OpenIddict 和 MongoDb 的解决方案。对于刚刚开始的人,以下内容可能会有所帮助。我的 OpenIddict/authentication/authorization 使用以下配置运行良好:

Startup.cs

配置服务:

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();
});

配置:

app.UseAuthentication();

app.UseCors("AllowBrowserApp");

app.UseRouting();

app.UseAuthorization();

注意:在哪里注册认证授权很重要。身份验证在 app.UseRouting() 之前,授权在之后。否则它不会工作。如果你使用 Visual Studio 它会显示给你。

UserEntity.cs

public class UserEntity : MongoUser
{
    public string Firstname { get; set; }

    public string Lastname { get; set; }
}

如果需要,您可以添加更多属性。

UserRoleEntity.cs

public class UserRoleEntity : MongoRole
{
    public UserRoleEntity() : base() { }

    public UserRoleEntity(string roleName) : base(roleName) { }
}