在ASP.NET 5中使用IdentityServer3.AccessTokenValidation的中间件?返回null对象异常

时间:2015-08-13 00:17:45

标签: c# owin asp.net-core openid-connect identityserver3

我正在尝试让IdentityServer3.AccessTokenValidation在ASP.NET 5 Web应用程序中工作,但下面的代码在我遗漏某些内容时抛出了一个空异常?

project.json - 依赖(部分)

"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" }
        });

扩展方法

    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;
            });
        });
    }

以下行继续抛出空对象异常但无法确定缺少的内容

builder.UseIdentityServerBearerTokenAuthentication(options);

堆栈跟踪

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)

我正在使用下面的身份服务器

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

1 个答案:

答案 0 :(得分:2)

我也有这个NullPointerException。问题是方法app.GetLoggerFactory(); (来自IAppBuilder)返回null。 在我的情况下,此方法在调用

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

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

因此,在app.Map之前放置方法builder.UseIdentityServerBearerTokenAuthentication(options);可以解决NullPointer问题。 但现在我遇到的问题是,似乎无法在同一个项目中托管WebApi和IdentityServer,但这是另一个问题..