【问题标题】:IdentityServer4 Quickstart issuesIdentityServer4 快速入门问题
【发布时间】:2018-12-08 03:58:28
【问题描述】:

我目前在 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 asp.net-core .net-core identityserver4


    【解决方案1】:

    如果您使用的是快速入门 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
    

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

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

    --

    下面是一套完整的步骤:

    现在修改您的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>();
    }
    

    【讨论】:

    • 您好,感谢您的回复!我试过了,但它只是给了我这个错误: InvalidOperationException: Unable to resolve service for type 'IdentityServer4.Stores.IClientStore' while trying to activate 'IdentityServer4.Validation.AuthorizeRequestValidator'。
    • 另外,如果我点击你给我的链接,它会要求我添加 app.useidentityserver()。这将导致此错误:'未指定客户端的存储机制。使用“AddInMemoryClients”扩展方法注册开发版本另外,您提供的链接没有显示在 Program 和 Startup.cs 中放置的内容
    • @JianYA 恐怕不确定;我只能说我遵循了 github 上的说明,忽略了 identityserver.io 上的文档,它对我有用吗?
    • 我可以看看你的 Program.cs 和 Startup.cs 吗?也许我必须跟随你的
    • 另外,快速入门文件是控制器吗?还是只是一个名为 Quickstart 的文件夹
    【解决方案2】:

    更新

    *查看此代码:它可能会有所帮助,因为我也在学习它,它对我有用。 *

    public void ConfigureServices(IServiceCollection services)
            {
                services.AddMvc();
                var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
    
                // configure identity server with in-memory stores, keys, clients and scopes
                services.AddIdentityServer()
                    .AddDeveloperSigningCredential()
                    .AddTestUsers(Config.GetUsers())
                    //.AddInMemoryClients(Config.GetClients())
                    // this adds the config data from DB (clients, resources)
                    .AddConfigurationStore(options =>
                    {
                        options.ConfigureDbContext = builder =>
                            builder.UseSqlServer(Configuration.GetConnectionString("IdentityConnectionString"), sql =>
                                sql.MigrationsAssembly(migrationsAssembly));
                    })
    
                    // this adds the operational data from DB (codes, tokens, consents)
                    .AddOperationalStore(options =>
                    {
                        options.ConfigureDbContext = builder =>
                            builder.UseSqlServer(Configuration.GetConnectionString("IdentityConnectionString"),
                                sql => sql.MigrationsAssembly(migrationsAssembly));
    
                        // this enables automatic token cleanup. this is optional.
                        options.EnableTokenCleanup = true;
                        options.TokenCleanupInterval = 30;
                    });
                    //.AddInMemoryIdentityResources(Config.GetIdentityResources())
                    //.AddInMemoryApiResources(Config.GetApiResources())
    
    
                services.AddAuthentication();
    
            }
    
    
        // clients want to access resources (aka scopes)
        public static IEnumerable<Client> GetClients()
        {
            // client credentials client
            return new List<Client>
            {
                new Client
                {
                    ClientId = "client",
                    AllowedGrantTypes = GrantTypes.ClientCredentials,
    
                    ClientSecrets =
                    {
                        new Secret("secret".Sha256())
                    },
                    AllowedScopes = { "api1" }
                },
    
                // resource owner password grant client
                new Client
                {
                    ClientId = "ro.client",
                    AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
    
                    ClientSecrets =
                    {
                        new Secret("secret".Sha256())
                    },
                    AllowedScopes = { "api1" }
                },
    
                // OpenID Connect hybrid flow and client credentials client (MVC)
    
    
            };
        }
    

    如果您不使用内存服务

    如果您收到此错误:无法解析类型“IdentityServer4.Stores.IClientStore”的服务

    明确注册商店和实现:(试一试)

        services.AddScoped<IUserStore<User>, UserService>();
        services.AddScoped<IClientStore, ClientService>();
        services.AddScoped<IScopeStore, ScopeService>();
        services.AddScoped<IPersistedGrantStore, GrantService>();
    

    【讨论】:

    • 嗨,它说我必须生成多个类。这些课程去哪里了?
    • 你在内存存储中使用吗?向我们展示您的身份验证启动
    • 我的创业公司发布在我的问题中。是的,我在内存存储中使用。
    • 我添加了我的身份验证的启动代码,查看它,它可能会有所帮助。
    猜你喜欢
    • 1970-01-01
    • 2020-08-21
    • 2018-04-03
    • 2018-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-22
    • 2023-03-31
    相关资源
    最近更新 更多