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智能机器人...
德州之星外 挂!德州nzt实战... 德州之星外 挂!德州nzt实战,云扑克德州辅助器免费(详细透视辅助工具黑科技)该软件可以轻松地帮助玩...
德扑之星有猫腻!德扑之星可以看... 德扑之星有猫腻!德扑之星可以看底牌,德扑ai都是真的有挂(详细透视辅助器教程)德扑之星有猫腻!德扑之...
德州之星插件!德州之星app有... 德州之星插件!德州之星app有,云扑克辅助器是真是假(详细透视辅助工具技巧)云扑克辅助器是真是假辅助...
德扑之星有作弊!德扑之星app... 德扑之星有作弊!德扑之星app发牌规律,德扑一贯真的有挂(详细透视辅助软件教程);一、德扑之星有作弊...
智星德州菠萝开挂!德州ai辅助... 智星德州菠萝开挂!德州ai辅助神器,pokermaster外 挂(详细透视辅助插件规律)1、该软件可...
德扑之星猫腻!德扑ai代打会检... 德扑之星猫腻!德扑ai代打会检测到,德扑平台竟然真的有挂(详细透视辅助脚本教程);一、德扑之星猫腻软...
德州ai辅助软件!德州ai辅助... 德州ai辅助软件!德州ai辅助app,智星德州菠萝成牌闯关(详细透视辅助器插件)1、这是跨平台的智星...
德扑之星ai代打!德扑软件决策... 德扑之星ai代打!德扑软件决策,德扑果然真的有挂(详细透视辅助app教程)1、下载好德扑之星ai代打...
德州免费辅助神器app!德州a... 德州免费辅助神器app!德州ai软件可以收费,云扑克外 挂(详细透视辅助软件插件)1、完成云扑克外 ...