我目前在 IdentityServer 4 指南 http://docs.identityserver.io/en/dev/quickstarts/3_interactive_login.html 的这个页面上,我正在尝试启动 MVC 应用程序。

但是,当我启动客户端应用程序时,我不断收到此错误

InvalidOperationException: Unable to resolve service for type 'IdentityServer4.Services.IIdentityServerInteractionService' while attempting to activate 'IdentityServer4.Quickstart.UI.HomeController'.

我进入了 IdentityServer4 GitHub 并从那里复制了代码,但它根本没有运行。

我不知道如何从这里开始。

这是我的 Startup.cs
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace IdentityServerClient
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        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();
        }
    }
}

asp.net - IdentityServer4 快速入门问题-LMLPHP
我也无法进入文档中显示的登录页面。

最佳答案

如果您使用的是快速入门 UI,您应该使用它的指南,这里是:

https://github.com/IdentityServer/IdentityServer4.Quickstart.UI

引用该页面:


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

        // some details omitted
        services.AddIdentityServer();

        services.AddAuthentication()
        ...

你不见了:
services.AddIdentityServer()
    .AddInMemoryCaching()
    .AddClientStore<InMemoryClientStore>()
    .AddResourceStore<InMemoryResourcesStore>(); // <-- Add this

因此,没有任何身份服务器服务注册到依赖项注入(inject)容器,这就是您看到该错误的原因。

您链接的教程文档似乎已过时。

——

以下是一组完整的步骤:
  • dotnet 新 sln -n HelloID4
  • dotnet 新 mvc -n HelloID4
  • dotnet sln 添加 HelloID4/HelloID4.csproj
  • cd HelloID4
  • git clone --depth 1 https://github.com/IdentityServer/IdentityServer4.Quickstart.UI
  • cp -r IdentityServer4.Quickstart.UI/* .
  • dotnet 添加包 IdentityServer4
  • rm -r Controller /
  • dotnet 监视运行

  • 现在将您的 Startup.cs 修改为如下所示:
    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        }); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    
        services.AddIdentityServer()
            .AddInMemoryCaching()
            .AddClientStore<InMemoryClientStore>()
            .AddResourceStore<InMemoryResourcesStore>();
    }
    

    关于asp.net - IdentityServer4 快速入门问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51093549/

    10-13 05:46