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

IdentityServer 4. Invalid operation Exception in MVC Client of IClientStore

我正在使用.NET核心2中的IdentityServer 4实现身份验证服务器,为了测试它,我使用了本教程http://docs.IdentityServer.io/en/release/quickstarts/3_interactive_login.html要实现MVC客户机,但它向我显示此错误:

InvalidOperationException: Unable to resolve service for type 'IdentityServer4.Stores.IClientStore' while attempting to activate 'IdentityServer4.Validation.AuthorizeRequestValidator'.

我试过很多东西,但都没用。

谢谢你

这是客户端MVC端:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 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;
            });
}

这是我的启动标识服务器端:

1
2
3
4
5
6
7
8
9
10
11
12
13
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配置){配置=配置;}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
    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();
    }
}

}

1
namespace Server

{公共类启动{//运行时调用此方法。使用此方法向容器中添加服务。//有关如何配置应用程序的详细信息,请访问https://go.microsoft.com/fwlink/?林奇=398940公共void配置服务(ISeviceCollection服务){services.addmvc();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
        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();

    }
}

}


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

services.AddIdentityServer();

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

编辑

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

例如,如果您有"iuserservice",则需要将其添加到IServiceCollection对象中。这样地:

1
services.AddTransient<IUserService, UserService>();