IdentityServer 4. IClientStore的MVC客户端中的无效操作异常

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

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

我尝试了很多东西,但没有一个能奏效 .

谢谢

这是Client MVC Side:

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;
            });
}

这是我的startUp IdentityServer Side:

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

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

    }
namespace ClientMVC

{public class Startup {public Startup(IConfiguration configuration)

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

{public class Startup {//此方法由运行时调用 . 使用此方法将服务添加到容器 . //有关如何配置应用程序的更多信息,请访问https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services){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();

    }
}

}

回答(1)

3 years ago

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

services.AddIdentityServer();

这应仅存在于Identity Server项目的启动中 . services.AddAuthentication(options =>... 是告诉MVC应用程序的一段代码,它受某些东西的保护,而且 AddOpenIdConnect 指定这是你的IdentityServer .

EDIT

通常,您在控制器中使用和注入的所有服务最初都应添加到 public void ConfigureServices(IServiceCollection services) 中 .

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

services.AddTransient<IUserService, UserService>();