IdentityServer4 icon indicating copy to clipboard operation
IdentityServer4 copied to clipboard

.NET Core 2.0 support

Open erwinwildenburg opened this issue 9 years ago • 30 comments

  • [x] I read and understood how to enable logging

Issue / Steps to reproduce the problem

Trying to authenticate a ASP.NET Core 2.0 project with IdentityServer4. However the authentication part of ASP.NET has undergone some major changes: https://github.com/aspnet/Announcements/issues/232

The only issue so far is:

Reference to type 'AuthenticationOptions' claims it is defined in 'Microsoft.AspNetCore.Authentication', but it could not be found

Startup.cs:

using IdentityServer4.AccessTokenValidation;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Serialization;
using System;
using System.IdentityModel.Tokens.Jwt;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Authentication.Cookies;

namespace API
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        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)
        {
            // Authenticate with IdentityServer4
            services.AddAuthentication(options => {
                options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
                options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            });

            // Add framework services
            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IServiceProvider serviceProvider)
        {
            // Authenticate with IdentityServer4
            JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
            app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
            {
                Authority = Configuration.GetValue<string>("Authentication:Authority"),
                ApiName = Configuration.GetValue<string>("Authentication:ApiName"),
                ApiSecret = Configuration.GetValue<string>("Authentication:ApiSecret"),
                SupportedTokens = SupportedTokens.Both,
                RequireHttpsMetadata = false
            });

            app.UseMvc();
        }
    }
}

Project.csproj:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <PreserveCompilationContext>true</PreserveCompilationContext>
  </PropertyGroup>
  <PropertyGroup>
    <PackageTargetFallback>$(PackageTargetFallback);dnxcore50;portable-net451+win8</PackageTargetFallback>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="IdentityServer4.AccessTokenValidation" Version="1.2.0" />
    <PackageReference Include="IdentityServer4.AspNetIdentity" Version="1.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0-preview1-final" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0-preview1-final" />
  </ItemGroup>
</Project>

Relevant parts of the log file

N/A

erwinwildenburg avatar May 15 '17 18:05 erwinwildenburg

@leastprivilege I still get the same error even with rc1

vitalbsk avatar Sep 11 '17 04:09 vitalbsk

Actually from IdentityServer4 Version 2.0.0 onwards (including preview releases and release candidates, the Configurations of AuthenticationServices have been moved to ConfigureServices method instead of Configure method in the Startup class.

Try using services.AddAuthentication(...).AddIdentityServerAuthentication(scheme, options) in ConfigureServices() method instead of the old one in the Configure() method.

I hope you understand this!.

technicalganesh avatar Sep 11 '17 14:09 technicalganesh

TechnicalGanesh:

I understand the code your are referring to in Startup.cs and I appreciate any other development snippets, pseudo-code or advice that you have to offer on the upgrade to IDSVR2.0

Thanks,

Robert Hyland

President & CEO,

Hyland Computer Systems, L.L.C

(E) [email protected]

(P) 720-616-9965

From: technicalganesh [mailto:[email protected]] Sent: Monday, September 11, 2017 8:43 AM To: IdentityServer/IdentityServer4 [email protected] Cc: Hyland Computer Systems, L.L.C. [email protected]; Comment [email protected] Subject: Re: [IdentityServer/IdentityServer4] .NET Core 2.0 support (#1173)

Actually from IdentityServer4 Version 2.0.0 onwards (including preview releases and release candidates, the Configuration of AuthenticationServices have been moved to ConfigureServices method instead of Configure method in the Startup class.

Try using services.AddAuthentication(...).AddIdentityServerAuthentication(scheme, options) in ConfigureServices() method instead of the old one in the Configure() method.

I hope you understand this!.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/IdentityServer/IdentityServer4/issues/1173#issuecomment-328551405 , or mute the thread https://github.com/notifications/unsubscribe-auth/ADtu5BXnHM_JBiwybPhXpPPluVvfO-W6ks5shUb2gaJpZM4NbgGf .

rhyland avatar Sep 11 '17 17:09 rhyland

You can find the QuickStart sample here

https://github.com/IdentityServer/IdentityServer4.Samples/tree/release/Quickstarts

Particularly at Hybrid flow authentication for your case!

technicalganesh avatar Sep 11 '17 19:09 technicalganesh

`app.UseJwtBearerAuthentication(new Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerOptions { Authority = authority, RequireHttpsMetadata = false,

        })`

The Microsoft's Middleware is obsolete now!. What am I supposed to do now?

waqaskhan540 avatar Sep 15 '17 06:09 waqaskhan540

@waqaskhan540 you can see from my demo here https://github.com/rizamarhaban/IdentityServer4Demo

rizamarhaban avatar Sep 15 '17 09:09 rizamarhaban

Are AutomaticAuthenticate and AutomaticChallenge obsolete for IdentityServerAuthenticationOptions?

I changed settings from

JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
            var identityServerValidationOptions = new IdentityServerAuthenticationOptions
            {
                Authority = Configuration.GetSection("IdentityServerSettings:Host").Value,
                AllowedScopes = new List<string> { "Api" },
                ApiSecret = "ApiSecret",
                ApiName = "Api",
                AutomaticAuthenticate = true,
                SupportedTokens = SupportedTokens.Both,
                AutomaticChallenge = true,
                RequireHttpsMetadata = false
            };

            app.UseIdentityServerAuthentication(identityServerValidationOptions);

to

            services.AddAuthentication()
                .AddIdentityServerAuthentication(x =>
                {
                    x.Authority = Configuration.GetSection("IdentityServerSettings:Host").Value;
                    x.AllowedScopes = new List<string> { "Api" };
                    x.ApiSecret = "ApiSecret";
                    x.ApiName = "Api";
                    x.SupportedTokens = SupportedTokens.Both;
                    x.RequireHttpsMetadata = false;
                });

nukec avatar Sep 20 '17 11:09 nukec

Addition to @nukec answer: you need for AddIdentityServerAuthentication install-package IdentityServer4.AccessTokenValidation (ver2.0+)

        services.AddAuthentication()
            .AddIdentityServerAuthentication(x =>
            {
                x.Authority = Configuration.GetSection("IdentityServerSettings:Host").Value;
                x.AllowedScopes = new List<string> { "Api" };
                x.ApiSecret = "ApiSecret";
                x.ApiName = "Api";
                x.SupportedTokens = SupportedTokens.Both;
                x.RequireHttpsMetadata = false;
            });

I had InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found. so I needed to add this:


services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
.AddIdentityServerAuthentication(x=>
{
                    x.Authority = "xxx";
                    x.ApiName = "xxx";
                    x.RequireHttpsMetadata = false;
});

and in configure method: app.UseAuthentication(); services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme) Adds the default schema

of course You'll need to add package and using IdentityServer4.AccessTokenValidation; This applies to IdentityServer4.AccessTokenValidation ver 2.1

future81 avatar Dec 04 '17 21:12 future81

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Jan 13 '20 17:01 lock[bot]