AspnetBoilerplate IdentityServer - 配置存储
创始人
2024-09-20 06:30:34
0

要解决"AspnetBoilerplate IdentityServer - 配置存储"的问题,并包含代码示例,您可以按照以下步骤进行操作:

步骤1:安装相关库 首先,您需要安装IdentityServer模块和AspnetBoilerplate框架。您可以使用NuGet包管理器或通过在项目文件中添加相关依赖项来安装它们。确保在项目中具有以下包:

  • Abp.IdentityServer4
  • IdentityServer4

步骤2:创建IdentityServer模块 然后,您需要创建一个名为IdentityServer的模块,并在其中定义IdentityServer的配置和存储。在您的项目中,创建一个名为IdentityServer的类库项目,然后在该项目中创建一个名为IdentityServerModule的类,并从AbpModule类继承。在IdentityServerModule类中,您可以配置IdentityServer以及其存储。

using Abp.Modules;
using IdentityServer4.Configuration;
using IdentityServer4.EntityFramework.DbContexts;
using IdentityServer4.EntityFramework.Mappers;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace IdentityServer
{
    [DependsOn(typeof(AbpIdentityServer4Module))]
    public class IdentityServerModule : AbpModule
    {
        private readonly IConfigurationRoot _appConfiguration;

        public IdentityServerModule(IWebHostEnvironment env)
        {
            _appConfiguration = env.GetAppConfiguration();
        }

        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            context.Services.AddIdentityServer()
                .AddDeveloperSigningCredential()
                .AddInMemoryIdentityResources(Config.IdentityResources)
                .AddInMemoryApiResources(Config.ApiResources)
                .AddInMemoryClients(Config.Clients)
                .AddAbpPersistedGrants()
                .AddAbpIdentityServer()
                .AddAbpApiAuthorization();

            Configure(options =>
            {
                options.UseSqlServer();
            });
        }

        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            var app = context.GetApplicationBuilder();
            var env = context.GetEnvironment();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseIdentityServer();
        }
    }
}

步骤3:创建IdentityServer数据库上下文 在上一步的示例代码中,我们使用了一个名为IdentityServerDbContext的上下文,它用于存储IdentityServer的配置。您需要创建一个类似于以下示例代码的上下文类:

using Abp.IdentityServer4;
using Abp.Zero.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace IdentityServer.EntityFrameworkCore
{
    public class IdentityServerDbContext : AbpZeroDbContext, IAbpPersistedGrantDbContext
    {
        public virtual DbSet PersistedGrants { get; set; }

        public IdentityServerDbContext(DbContextOptions options)
            : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.ConfigurePersistedGrantEntity();
            base.OnModelCreating(modelBuilder);
        }
    }
}

步骤4:配置IdentityServer 在上面的示例代码中,我们使用了一个名为Config的静态类来配置IdentityServer的资源和客户端。您需要创建一个名为Config的类,并在其中配置IdentityServer的资源和客户端。以下是一个示例:

using IdentityServer4.Models;
using System.Collections.Generic;

namespace IdentityServer
{
    public class Config
    {
        public static IEnumerable IdentityResources =>
            new List
            {
                new IdentityResources.OpenId(),
                new IdentityResources.Profile(),
                new IdentityResources.Email(),
            };

        public static IEnumerable ApiResources =>
            new List
            {
                new ApiResource("api1", "My API")
            };

        public static IEnumerable Clients =>
            new List
            {
                new Client
                {
                    ClientId = "client",
                    AllowedGrantTypes = GrantTypes.ClientCredentials,
                    ClientSecrets =
                    {
                        new Secret("secret".Sha256())
                    },
                    AllowedScopes = { "api1" }
                }
            };
    }
}

步骤5:启用IdentityServer模块 最后,您需要在您的

相关内容

热门资讯

1分钟辅助挂!川娱竞技辅助,鄱... 1分钟辅助挂!川娱竞技辅助,鄱阳翻精辅助软件,存在挂教程(原来是真的有挂)所有人都在同一条线上,像星...
第4分钟辅助!掌中乐游戏中心辅... 第4分钟辅助!掌中乐游戏中心辅助器,情怀莆仙吹牛脚本,揭秘教程(都是真的是有挂);1)掌中乐游戏中心...
九分钟了解!菜鸟黑桃a3作弊,... 九分钟了解!菜鸟黑桃a3作弊,大巴杭州辅助,靠谱教程(真是真的是有挂);1、金币登录送、破产送、升级...
2分钟了解!禅游指尖四川修改器... 2分钟了解!禅游指尖四川修改器,决战卡五星辅助器,技巧教程(本来存在有挂)1、该软件可以轻松地帮助玩...
三分钟了解!赣牌圈修改器,新道... 三分钟了解!赣牌圈修改器,新道游app辅助器,玩家教你(确实是有挂)1、新道游app辅助器系统规律教...
六分钟了解!家乡大二辅助免费,... 六分钟了解!家乡大二辅助免费,山西扣点辅助工具,软件教程(其实是真的有挂)1、完成山西扣点辅助工具的...
2分钟辅助挂!广东雀神智能插件... 2分钟辅助挂!广东雀神智能插件,鱼乐吧游戏脚本,软件教程(都是是有挂)所有人都在同一条线上,像星星一...
第五分钟辅助!朱雀开心罗松辅助... 第五分钟辅助!朱雀开心罗松辅助器,财神十三张脚本效果图,存在挂教程(好像有挂)1、财神十三张脚本效果...
7分钟了解!多乐手游辅助,安庆... 7分钟了解!多乐手游辅助,安庆胡乐麻将辅助器,2025教程(总是真的有挂);运安庆胡乐麻将辅助器辅助...
三分钟了解!西兵互娱辅助插件a... 三分钟了解!西兵互娱辅助插件app,老友辅助工具下载,科技教程(竟然真的是有挂)1、全新机制【西兵互...