Madu Alikor
Madu Alikor

Reputation: 2662

owin middlware with IdentityServer3.AccessTokenValidation in ASP.NET 5? returns null object exception

I am trying to get the IdentityServer3.AccessTokenValidation to work in a ASP.NET 5 Web application but the code below throws a null exception am I missing something?

project.json - dependencies (partial)

"IdentityServer3.AccessTokenValidation": "2.0.0-build00019",
"Microsoft.AspNet.WebApi.Owin": "5.2.3",
"Microsoft.Owin.Host.SystemWeb": "3.0.1",
"Microsoft.AspNet.Authentication.OAuthBearer": "1.0.0-beta6",
"Microsoft.Owin.Security.OAuth": "3.0.0",
"Microsoft.AspNet.Owin": "1.0.0-beta6",
"Microsoft.AspNet.Authorization": "1.0.0-beta6",
"Microsoft.IdentityModel.Protocol.Extensions": "1.0.0",
"Newtonsoft.Json": "6.0.6"

Startup.cs

        app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
        {
            Authority = "https://localhost:44300/",
            RequiredScopes = new[] { "api1" }
        });

Extension method

    public static void UseIdentityServerBearerTokenAuthentication(this IApplicationBuilder app, IdentityServerBearerTokenAuthenticationOptions options)
    {
        app.UseOwin(addToPipeline =>
        {
            addToPipeline(next =>
            {
                var builder = new Microsoft.Owin.Builder.AppBuilder();

                builder.UseIdentityServerBearerTokenAuthentication(options);

                var appFunc = builder.Build(typeof(Func<IDictionary<string, object>, Task>)) as Func<IDictionary<string, object>, Task>;
                return appFunc;
            });
        });
    }

The following line keeps on throwing a null object exception but unable to determine what is missing

builder.UseIdentityServerBearerTokenAuthentication(options);

Stacktrace

At IdentityServer3.AccessTokenValidation.DiscoveryDocumentIssuerSecurityTokenProvider..ctor(String discoveryEndpoint, IdentityServerBearerTokenAuthenticationOptions options, ILoggerFactory loggerFactory) in c:\projects\thinktecture-identityserver-v3-  accesstokenvalidati\source\AccessTokenValidation\Plumbing\DiscoveryDocumentIssuerSecurityTokenProvider.cs:line 43
at Owin.IdentityServerBearerTokenValidationAppBuilderExtensions.ConfigureLocalValidation(IdentityServerBearerTokenAuthenticationOptions options, ILoggerFactory loggerFactory) in c:\projects\thinktecture-identityserver-v3-accesstokenvalidati\source\AccessTokenValidation\IdentityServerBearerTokenValidationAppBuilderExtensions.cs:line 100
at Owin.IdentityServerBearerTokenValidationAppBuilderExtensions.UseIdentityServerBearerTokenAuthentication(IAppBuilder app, IdentityServerBearerTokenAuthenticationOptions options) in c:\projects\thinktecture-identityserver-v3-accesstokenvalidati\source\AccessTokenValidation\IdentityServerBearerTokenValidationAppBuilderExtensions.cs:line 50
at Portal.IdentityServerAccessTokenValidationAppBuilderExtensions.<>c__DisplayClass0_0.<UseIdentityServerBearerTokenAuthentication>b__1(Func`2 next) in C:\code\Sense.Care\src\Portal\Configuration\IdentityServerAccessTokenValidationAppBuilderExtensions.cs:line 23
at Microsoft.AspNet.Builder.OwinExtensions.<>c__DisplayClass0_1.<UseOwin>b__1(RequestDelegate next1)
at Microsoft.AspNet.Builder.Internal.ApplicationBuilder.Build()
at Microsoft.AspNet.Hosting.Internal.HostingEngine.BuildApplication()
at Microsoft.AspNet.Hosting.Internal.HostingEngine.Start()
at Microsoft.AspNet.Loader.IIS.RuntimeHttpApplication.ApplicationStart(IHttpApplication application)
at Microsoft.AspNet.Loader.IIS.HttpApplicationBase.InvokeApplicationStart(IHttpApplication application)

I am using the identity server below

https://github.com/IdentityServer/IdentityServer3.Samples/tree/master/source/AspNet5Host

Upvotes: 1

Views: 2203

Answers (1)

huer12
huer12

Reputation: 314

I also had this NullPointerException. The issue is that the method app.GetLoggerFactory(); (from the IAppBuilder) returns null. In my case this method returns null after I call

app.Map("/admin", adminApp =>
        {
            var factory = new IdentityManagerServiceFactory();
            factory.ConfigureSimpleIdentityManagerService();

            adminApp.UseIdentityManager(new IdentityManagerOptions()
            {
                Factory = factory
            });
        });

So placing the the method builder.UseIdentityServerBearerTokenAuthentication(options); before the app.Map solves the NullPointer issue. But now I have the problem that it seems that it is not possible to host the WebApi and the IdentityServer in the same project, but that's another issue..

Upvotes: 2

Related Questions