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

时间:2018-04-23 13:17:43

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

我正在.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;
            });
}

这是我的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

{     公共课启动     {         公共启动(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         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 个答案:

答案 0 :(得分:1)

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

  

services.AddIdentityServer();

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

修改

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

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

services.AddTransient<IUserService, UserService>();