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(wepoker)... 透视智能ai(wepoker)外挂透明挂辅助工具(wepokeai辅助)确实是真的有挂(详细透视普及...
德扑之星猫腻!哈糖大菠萝切牌规... 德扑之星猫腻!哈糖大菠萝切牌规律,wpk被系统针对,科技教程(有挂教学)1、这是跨平台的德扑之星猫腻...
透视模拟器(WepOke)透明... 透视模拟器(WepOke)透明挂辅助工具(WePoKe透明挂)确实真的有挂(详细透视解说技巧)是由北...
WPK透视辅助!pokerx人... WPK透视辅助!pokerx人工智能,pokerwoeld安卓下载,存在挂教程(有挂透明)1、金币登...
透视了解(wepOke)透视辅... 自定义WePoKe外挂系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器,不管...
wpk外挂!德扑自定义房间,w... 一、wpk外挂简介了解软件请加微:136704302wpk外挂是一款在线扑克游戏平台,玩家可以在平台...
透视攻略(wePOKE)外挂透... 透视攻略(wePOKE)外挂透明挂辅助工具(WePoKe透视挂)本然是真的有挂(详细透视靠谱教程);...
微扑克ai机器人!wpk俱乐部... 微扑克ai机器人!wpk俱乐部有ai吗,微扑克插件,介绍教程(有挂透明)是一款可以让一直输的玩家,快...
透视游戏(wEpoKe)外挂透... 1、透视游戏(wEpoKe)外挂透明挂辅助挂(wepoke黑科技)确实是有挂(详细透视安装教程)。2...
德扑ai助手!微扑克这软件有问... 德扑ai助手!微扑克这软件有问题吗,wepoke真的有挂嘛,新2025版(有挂攻略)是一款可以让一直...