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

相关内容

热门资讯

最新技巧!哈糖大菠萝有挂吗,w... 最新技巧!哈糖大菠萝有挂吗,wepoker私局辅助,可靠技巧(有挂软件);实战中需综合运用上述技巧,...
玩家必看教程!hhpoker怎... 玩家必看教程!hhpoker怎么防作弊,wepoker国外版透视,透明挂教程(有挂方法)是一款可以让...
玩家必看分享!wepoker透... 玩家必看分享!wepoker透视挂底牌,hhpoker可以控制牌吗,德州论坛(有挂技巧)1、不需要A...
今日公布!德州局透视脚本免费版... 今日公布!德州局透视脚本免费版下载手机版,wepoker私人局怎么玩,攻略教程(有挂技巧);玩家必备...
终于懂了!wepoker辅助器... 终于懂了!wepoker辅助器有哪些功能,哈糖大菠萝软件下载,解说技巧(有挂方法);实战中需综合运用...
科普攻略!hhpoker有作弊... 科普攻略!hhpoker有作弊的吗,拱趴大菠萝万能辅助器,曝光教程(有挂透视);建议优先通过拱趴大菠...
专业讨论!aapoker透视脚... 专业讨论!aapoker透视脚本下载,wepoker透视有吗,详细教程(有挂透明)是一款可以让一直输...
记者揭秘!佛手大菠萝辅助,we... 记者揭秘!佛手大菠萝辅助,wepoker辅助软件视频,德州论坛(有挂辅助);实战中需综合运用上述技巧...
新手必备!竞技联盟破解版最新版... 1、新手必备!竞技联盟破解版最新版,wepoker到底有没有透视,切实教程(有挂透明);详细教程。2...
总算了解!智星德州可以透视吗,... 总算了解!智星德州可以透视吗,sohoo辅助,分享教程(有挂方法);《WPK辅助透视》‌:支持手机实...