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

相关内容

热门资讯

二分钟了解!老友麻将,同城游5... 二分钟了解!老友麻将,同城游510k有挂吗,解密教程(有挂方法)1、进入游戏-大厅左侧-新手福利-激...
必备攻略!全民牛牛拼三张怎么提... 必备攻略!全民牛牛拼三张怎么提高胜率(透视辅助)确实真的有挂(2020已更新)(哔哩哔哩);全民牛牛...
九分钟了解!广西八一字牌是不是... 九分钟了解!广西八一字牌是不是有挂,财神13张怎么拿到好牌,辅助教程(有挂实操)1、广西八一字牌是不...
今日公布!钱塘十三水开挂检测得... 今日公布!钱塘十三水开挂检测得出来吗(透视辅助)好像真的有挂(2021已更新)(哔哩哔哩);1、不需...
8分钟了解!边锋杭麻圈辅助,中... 8分钟了解!边锋杭麻圈辅助,中至余干麻将,细节方法(有挂工具);1.边锋杭麻圈辅助 ai辅助创建新账...
玩家必用!欢乐达人辅助器(透明... 玩家必用!欢乐达人辅助器(透明挂)一直存在有挂(2022已更新)(哔哩哔哩)1、不需要AI权限,帮助...
九分钟了解!星悦云南麻将怎么让... 九分钟了解!星悦云南麻将怎么让系统发好牌,心悦踢坑系统如何能给好牌,规律教程(有挂教学)1、星悦云南...
推荐一款!优乐麻将规律(透明挂... 推荐一款!优乐麻将规律(透明挂)其实是有挂(2023已更新)(哔哩哔哩)1、下载好优乐麻将规律辅助软...
六分钟了解!jj斗地主助赢神器... 六分钟了解!jj斗地主助赢神器,老友汇软件神器,必胜教程(有挂细节)1)jj斗地主助赢神器辅助挂:进...
最新技巧!潮汕暗宝可以作假吗(... 最新技巧!潮汕暗宝可以作假吗(透视辅助)竟然是真的有挂(2025已更新)(哔哩哔哩)1、进入游戏-大...