简体   繁体   English

.NET 核心配置 OpenIddict 与 MongoDb

[英].NET Core configure OpenIddict with MongoDb

For my REST Api I want to use the OpenIddict authentication scheme.对于我的 REST Api 我想使用 OpenIddict 身份验证方案。 As database I am using MongoDb.作为数据库,我使用的是 MongoDb。 I have installed all necessary dependencies.我已经安装了所有必要的依赖项。 All dependencies have the newest version.所有依赖项都具有最新版本。

In my Startup.cs I want to register OpenIddict now.在我的 Startup.cs 中,我现在想注册 OpenIddict。 In the first step make this (like in the documentaion)在第一步中做这个(就像在文档中一样)

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

Right on options.UseOpenIddict<ObjectId>();就在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.这是CS1061错误。

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?有谁知道如何解决这个问题?

The Problem was with a package that overrode the UseOpenIddict method.问题出在覆盖UseOpenIddict方法的 package 上。 I have uninstalled the package an rewrote some code, since the package wasn't that necessary.我已经卸载了 package 并重写了一些代码,因为 package 不是必需的。 This is the package that isn't compatible. 是不兼容的 package。

Update更新

Thank you Kévin Chalet for this comment .感谢Kévin Chalet评论

I rewrote my identity configuration to我将身份配置重写为

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

This works now perfectly for me.现在这对我来说非常有效。

Update 2更新 2

I have googled some more and I haven't found any solutions on how to properly implement OpenIddict and MongoDb.我在谷歌上搜索了更多信息,但没有找到任何关于如何正确实施 OpenIddict 和 MongoDb 的解决方案。 For someone who is just starting the following might help.对于刚刚开始以下内容的人可能会有所帮助。 My OpenIddict / authentication / authorization runs fine with the following configuration:我的 OpenIddict / authentication / authentication 使用以下配置运行良好:

Startup.cs启动.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.身份验证在app.UseRouting()之前,然后是授权。 Otherwise it won't work.否则它将无法正常工作。 If you use Visual Studio it will show you.如果您使用 Visual Studio,它将向您展示。

UserEntity.cs用户实体.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用户角色实体.cs

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM