【问题标题】:IdentityServer 4. Invalid operation Exception in MVC Client of IClientStoreIdentityServer 4. IClientStore的MVC客户端出现无效操作异常
【发布时间】:2018-04-23 13:17:42
【问题描述】:

我正在.net core 2 中使用 IdentityServer 4 实现身份验证服务器并对其进行测试,我使用了本教程 http://docs.identityserver.io/en/release/quickstarts/3_interactive_login.html 实现 MVC 客户端,但它向我显示此错误:

InvalidOperationException:尝试激活“IdentityServer4.Validation.AuthorizeRequestValidator”时无法解析“IdentityServer4.Stores.IClientStore”类型的服务。

我尝试了很多东西,但都没有奏效。

谢谢

这是客户端 MVC 端:

 public void ConfigureServices(IServiceCollection services)
 {
        services.AddIdentityServer();

        services.AddMvc();

        JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

        services.AddAuthentication(options =>
        {
            options.DefaultScheme = "Cookies";
            options.DefaultChallengeScheme = "oidc";
        })
            .AddCookie("Cookies")
            .AddOpenIdConnect("oidc", options =>
            {
                options.SignInScheme = "Cookies";
                options.Authority = "http://localhost:5000";
                options.RequireHttpsMetadata = false;

                options.ClientId = "mvc";
                options.SaveTokens = true;
            });
}

这是我的启动 IdentityServer 端:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();

        services.AddIdentityServer()
                 .AddDeveloperSigningCredential()
                 .AddInMemoryApiResources(Config.GetApiResources())
                 .AddInMemoryIdentityResources(Config.GetIdentityResources())
                 .AddInMemoryClients(Config.GetClients())
                 .AddTestUsers(Config.GetUsers());

    }
namespace ClientMVC

{ 公共类启动 { 公共启动(IConfiguration 配置) { 配置=配置; }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {

        services.AddMvc();

        JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

        services.AddAuthentication(options =>
        {
            options.DefaultScheme = "Cookies";
            options.DefaultChallengeScheme = "oidc";
        })
            .AddCookie("Cookies")
            .AddOpenIdConnect("oidc", options =>
            {
                options.SignInScheme = "Cookies";

                options.Authority = "http://localhost:5000";
                options.RequireHttpsMetadata = false;

                options.ClientId = "mvc";
                options.SaveTokens = true;
            });




    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }
        app.UseAuthentication();

        app.UseStaticFiles();
        app.UseMvcWithDefaultRoute();
    }
}

}

namespace Server

{ 公共类启动 { // 这个方法被运行时调用。使用此方法向容器添加服务。 // 有关如何配置应用程序的更多信息,请访问https://go.microsoft.com/fwlink/?LinkID=398940 公共无效配置服务(IServiceCollection 服务) { services.AddMvc();

        services.AddIdentityServer()
                 .AddDeveloperSigningCredential()
                 .AddInMemoryApiResources(Config.GetApiResources())
                 .AddInMemoryIdentityResources(Config.GetIdentityResources())
                 .AddInMemoryClients(Config.GetClients())
                 .AddTestUsers(Config.GetUsers());

    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    { 
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseIdentityServer();
        app.UseStaticFiles();
        app.UseMvcWithDefaultRoute();

    }
}

}

【问题讨论】:

  • 添加您的 startup.cs,尤其是您配置 IdentityServer 的位置。
  • 我在@RuardvanElburg 的帖子中添加了它
  • 嗨,这个问题为你解决了吗

标签: .net asp.net-mvc dependency-injection identityserver4 .net-core-2.0


【解决方案1】:

在您提供的 MVC 客户端配置中,删除:

services.AddIdentityServer();

这应该只存在于 Identity Server 项目的启动中。 services.AddAuthentication(options =>... 是一段代码,它告诉 MVC 应用程序它受到某种东西的保护,此外,AddOpenIdConnect 指定这是您的 IdentityServer。

编辑

一般来说,您在控制器中使用和注入的所有服务,最初都应该添加到public void ConfigureServices(IServiceCollection services) 中。

例如,如果您有一个“IUserService”,则需要将其添加到 IServiceCollection 对象中。像这样:

services.AddTransient<IUserService, UserService>();

【讨论】:

  • 我确实像你说的 @m3n7alsnak3 但我现在有这个:InvalidOperationException: Unable to resolve service for type 'IdentityServer4.Services.IIdentityServerInteractionService' 同时尝试激活 '.Controllers.Home.HomeController'。
  • @aouselkadhi - 检查编辑,而且我认为你搞砸了客户端和服务器。你能从两个startup.cs 文件中显示更多代码吗?更好地显示整个文件(当然你可以跳过 usings :))
  • 它就像你在帖子中所说的那样添加:)
  • 不要将其添加到我的答案中 - 将其添加到您的问题中
  • 它现在已添加到我的问题中。谢谢@m3n7alsnak3
猜你喜欢
  • 2020-08-06
  • 1970-01-01
  • 1970-01-01
  • 2011-11-06
  • 1970-01-01
  • 2017-05-26
  • 2010-10-02
  • 1970-01-01
相关资源
最近更新 更多