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模块 最后,您需要在您的

相关内容

热门资讯

分享开挂!新二号辅助下载,微乐... 微乐手游脚本辅助是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用户可以加我微...
曝光辅助!雀友会钻石辅助器潮汕... 您好:雀友会钻石辅助器潮汕麻将这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多...
发现开挂!雀友会广东潮汕辅助透... 发现开挂!雀友会广东潮汕辅助透视,樱花之盛能不能开挂(盘点一款开挂辅助下载);1、很好的雀友会广东潮...
正品辅助!微信小程序财神十三章... 正品辅助!微信小程序财神十三章特殊牌,蜀渝牌乐汇挂机软件(推荐一款开挂辅助工具)【无需打开直接搜索加...
分享辅助!皇豪互娱科技,aap... aapoker怎么控制牌开挂教程视频分享装挂详细步骤在当今的网络游戏中,aapoker怎么控制牌作为...
推荐开挂!全游大厅拼十辅助,w... 推荐开挂!全游大厅拼十辅助,wepoker新号好一点吗(我来向大家传授开挂辅助安装);无需打开直接搜...
正版开挂!新九哥辅助,九江讨赏... 正版开挂!新九哥辅助,九江讨赏辅助中至小程序(透视辅助开挂辅助安装)>>您好:软件加13670430...
发现开挂!新超圣正版辅助,aa... >>您好:aapoker安装包怎么使用确实是有挂的,很多玩家在这款aapoker安装包怎么使用游戏中...
通报开挂!随意玩透视辅助软件,... 通报开挂!随意玩透视辅助软件,道游互娱透视辅助截图(最新技巧开挂辅助脚本) >>您好:软件加薇136...
正品开挂!新道游戏辅助器安装包... 您好:这款新道游戏辅助器安装包游戏是可以开挂的,确实是有挂的,很多玩家在这款新道游戏辅助器安装包游戏...