ASP.NET Core v1.1中IdentityServer和Simple Injector的集成
创始人
2024-09-15 12:30:39
0

要在ASP.NET Core v1.1中集成IdentityServer和Simple Injector,您需要执行以下步骤:

  1. 首先,安装必要的NuGet包。在项目的.csproj文件中,添加以下包引用:

  
  
  
  
  

  1. 接下来,您需要创建一个IdentityServer的配置类。在项目中创建一个名为IdentityServerConfig.cs的类,并添加以下代码:
using IdentityServer4;
using IdentityServer4.Models;
using System.Collections.Generic;

public static class IdentityServerConfig
{
    public static IEnumerable GetIdentityResources()
    {
        return new List
        {
            new IdentityResources.OpenId(),
            new IdentityResources.Profile(),
            new IdentityResources.Email(),
        };
    }

    public static IEnumerable GetApiResources()
    {
        return new List
        {
            new ApiResource("api1", "My API"),
        };
    }

    public static IEnumerable GetClients()
    {
        return new List
        {
            new Client
            {
                ClientId = "client",
                AllowedGrantTypes = GrantTypes.ClientCredentials,
                ClientSecrets =
                {
                    new Secret("secret".Sha256())
                },
                AllowedScopes = { "api1" }
            },
        };
    }
}
  1. Startup.cs文件中,配置IdentityServer和Simple Injector的集成。在ConfigureServices方法中添加以下代码:
public void ConfigureServices(IServiceCollection services)
{
    // ... 其他配置代码 ...

    // 添加IdentityServer
    services.AddIdentityServer()
        .AddDeveloperSigningCredential()
        .AddInMemoryIdentityResources(IdentityServerConfig.GetIdentityResources())
        .AddInMemoryApiResources(IdentityServerConfig.GetApiResources())
        .AddInMemoryClients(IdentityServerConfig.GetClients())
        .AddAspNetIdentity();

    // 使用Simple Injector替换默认的依赖注入容器
    var container = new Container();
    container.Options.DefaultScopedLifestyle = new AspNetRequestLifestyle();
    container.RegisterMvcControllers(app);
    container.RegisterMvcViewComponents(app);
    container.ConfigureAspNetCore(app);
    services.AddSingleton(
        new SimpleInjectorControllerActivator(container));
    services.AddSingleton(
        new SimpleInjectorViewComponentActivator(container));
    services.UseSimpleInjectorAspNetRequestScoping(container);

    // 将Simple Injector容器设置为默认服务提供程序
    services.AddSingleton(container);
    services.AddSingleton(
        new SimpleInjectorDependencyResolver(container));

    // ... 其他配置代码 ...
}
  1. 最后,在Configure方法中启用IdentityServer和Simple Injector。添加以下代码:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    // ... 其他配置代码 ...

    // 使用IdentityServer中间件
    app.UseIdentityServer();

    // 使用Simple Injector中间件
    app.UseSimpleInjectorAspNetRequestScoping(container);

    // ... 其他配置代码 ...
}

这样,您就成功地将IdentityServer和Simple Injector集成到ASP.NET Core v1.1中了。请注意,以上代码示例仅供参考,您可能需要根据您的具体需求进行适当的调整和修改。

相关内容

热门资讯

透视好友(微扑克ai辅助)轰趴... 透视好友(微扑克ai辅助)轰趴大菠萝怎么提高胜率(详细辅助细节揭秘)起初真的有挂,亲,有的,ai轻松...
微扑克有辅助挂!aapoker... 微扑克有辅助挂!aapoker辅助,(AAPOKeR)起初真的有挂(详细辅助黑科技教程);原来确实真...
透视教程(微扑克ai辅助工具)... 透视教程(微扑克ai辅助工具)德扑之星隐藏功能(详细辅助透明挂教程)固有真的是有挂;一、AI软件牌型...
aapoker猫腻!wpk微扑... 《aapoker猫腻软件透明挂》是一款多人竞技的aapoker猫腻辅助透视游戏,你将微扑克对手来到同...
微扑克辅助软件!鱼扑克app ... 微扑克辅助软件!鱼扑克app ai,(wepokE)一贯存在有挂(详细辅助攻略教程);支持2-10人...
透视辅助(德州微扑克辅助)欢乐... 透视辅助(德州微扑克辅助)欢乐棋牌有没有挂(详细辅助新版2025教程)往昔真的有挂;是一种具有地方特...
透视游戏(德扑之星ai代打)g... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加Q群【1067239143】很多玩家在这款游戏中打...
德扑ai智能机器人!cloud... 德扑ai智能机器人!cloudpoker云扑克,(AAPOKeR)确实是真的有挂(详细辅助爆料教程)...
透视软件(aapoker外挂)... 透视软件(aapoker外挂)菠萝德州偷偷看功能(详细辅助必备教程)都是是真的有挂;无聊就玩这款真的...
wpk德州透视辅助!微扑克软件... wpk德州透视辅助赢率提升策略‌;wpk德州透视辅助!微扑克软件开发定制app,(德扑之星)一贯真的...