You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 5 Web API 集成OpenIdDict搭建授权服务 无EF自定义存储配置问题

OpenIddict 搭配 ASP.NET Core Identity 自定义存储实现方案

核心实现逻辑

OpenIddict 本身不强制依赖 Entity Framework,只需要实现其定义的4个核心存储接口,即可替换默认的EF存储实现,具体步骤如下:

步骤1:定义OpenIddict核心实体类

首先创建4个普通POCO类,对应OpenIddict的核心数据模型,不需要继承任何EF相关的基类,按需保留所需属性即可:

public class CustomOpenIddictApplication
{
    public string Id { get; set; }
    public string ClientId { get; set; }
    public string ClientSecret { get; set; }
    public string DisplayName { get; set; }
    public string RedirectUris { get; set; }
    public string Permissions { get; set; }
    // 其他需要的属性参考OpenIddict接口要求补充即可
}
// 同理创建 CustomOpenIddictAuthorization、CustomOpenIddictScope、CustomOpenIddictToken 三个类

步骤2:实现4个核心存储接口

按照你之前实现IUserStore、IRoleStore的相同逻辑,基于你现有的SqlConnection操作,实现OpenIddict的4个存储接口:

  • IOpenIddictApplicationStore<CustomOpenIddictApplication>
  • IOpenIddictAuthorizationStore<CustomOpenIddictAuthorization>
  • IOpenIddictScopeStore<CustomOpenIddictScope>
  • IOpenIddictTokenStore<CustomOpenIddictToken>
    每个接口的方法对应你自有数据库表的增删改查逻辑即可,不需要额外适配EF的规则。

步骤3:修改Startup注册逻辑

调整你的ConfigureServices方法,在AddOpenIddict中先配置自定义存储,再配置服务端参数:

public void ConfigureServices(IServiceCollection services)
{
    services.AddTransient<IUserStore<ApplicationUser>, UserStore>();
    services.AddTransient<IRoleStore<ApplicationRole>, RoleStore>();
    services.AddIdentity<ApplicationUser, ApplicationRole>()                
       .AddDefaultTokenProviders();
       
    string connectionString = Configuration.GetConnectionString("DefaultConnection");
    services.AddTransient<SqlConnection>(e => new SqlConnection(connectionString));

    services.AddOpenIddict()
        // 注册自定义存储核心配置
        .AddCore(options =>
        {
            // 指定自定义实体类型
            options.SetDefaultApplicationEntity<CustomOpenIddictApplication>();
            options.SetDefaultAuthorizationEntity<CustomOpenIddictAuthorization>();
            options.SetDefaultScopeEntity<CustomOpenIddictScope>();
            options.SetDefaultTokenEntity<CustomOpenIddictToken>();
            
            // 注册自定义存储实现
            options.Services.AddTransient<IOpenIddictApplicationStore<CustomOpenIddictApplication>, CustomApplicationStore>();
            options.Services.AddTransient<IOpenIddictAuthorizationStore<CustomOpenIddictAuthorization>, CustomAuthorizationStore>();
            options.Services.AddTransient<IOpenIddictScopeStore<CustomOpenIddictScope>, CustomScopeStore>();
            options.Services.AddTransient<IOpenIddictTokenStore<CustomOpenIddictToken>, CustomTokenStore>();
        })
        // 原有服务端配置保持不变
        .AddServer(options =>
        {
            options.AllowClientCredentialsFlow();
            options.SetTokenEndpointUris("connect/token");
            options
                .AddEphemeralEncryptionKey()
                .AddEphemeralSigningKey();
            options.RegisterScopes("api");
            options.UseAspNetCore().EnableTokenEndpointPassthrough();
        });

    services.AddControllers();
    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new OpenApiInfo { Title = "ServerAPI", Version = "v1" });
    });
}

注意事项

  • 请确保你使用的OpenIddict版本为3.x系列,更高版本的OpenIddict已停止支持.NET 5,会出现兼容性问题。
  • 自定义Identity存储和OpenIddict存储是完全独立的两套实现,互不影响,各自实现对应的接口即可正常运行。
  • 你提到的ASOS相关文档已过时,ASOS组件已经完全合并到OpenIddict项目中,无需参考旧文档。

内容的提问来源于stack exchange,提问作者Bala P

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.16 00:24:02