Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IdentityServer4 Quickstart issues

I am currently at this page of the IdentityServer 4 guide http://docs.identityserver.io/en/dev/quickstarts/3_interactive_login.html and I am trying to start the MVC application.

However, I keep getting this error when I start my Client application

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

I went into the IdentityServer4 GitHub and copied the code from there but it doesn't run at all.

I am not sure how to proceed from here.

This is my 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();
        }
    }
}

enter image description here I can't get to the login page shown on the documentation as well.

like image 755
JianYA Avatar asked Dec 18 '22 22:12

JianYA


1 Answers

If you're using the quickstart UI, you should be using the guide on it, here:

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

To quote that page:

Next you need to configure the authentication handlers:

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

        // some details omitted
        services.AddIdentityServer();

        services.AddAuthentication()
        ...  

You're missing:

services.AddIdentityServer()
    .AddInMemoryCaching()
    .AddClientStore<InMemoryClientStore>()
    .AddResourceStore<InMemoryResourcesStore>(); // <-- Add this

As a result, none of the identity server services are registered to the dependency injection container, which is why you're seeing that error.

Looks like the tutorial documentation you linked to is out of date.

--

Here is a complete set of steps to follow:

  • dotnet new sln -n HelloID4
  • dotnet new mvc -n HelloID4
  • dotnet sln add HelloID4/HelloID4.csproj
  • cd HelloID4
  • git clone --depth 1 https://github.com/IdentityServer/IdentityServer4.Quickstart.UI
  • cp -r IdentityServer4.Quickstart.UI/* .
  • dotnet add package IdentityServer4
  • rm -r Controllers/
  • dotnet watch run

Now modify your Startup.cs to look like this:

// 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>();
}
like image 91
Doug Avatar answered Dec 21 '22 23:12

Doug