A task was canceled
Asked Answered
C

1

3

I am trying to co-host identityserver3 and web api (for user management using Bearer tokens) in the same startup. However I get the following error: A task was canceled. It appears the task cancellation occurs on startup when trying to call http://identity_local/core/.well-known/openid-configuration (identity_local points to localhost).

My startup is as follows:

app.Map("/core", idsrvApp =>
        {
            var factory = new IdentityServerServiceFactory();
            factory.UserService = new IdentityServer3.Core.Configuration.Registration<IUserService, UserService>();
            factory.ScopeStore = new IdentityServer3.Core.Configuration.Registration<IScopeStore>(resolver => scopeStore);
            var options = new IdentityServerOptions
            {
                SigningCertificate = Certificate.Load(),
                IssuerUri = "http://identity_local/core",
                PublicOrigin = "http://identity_local",
                RequireSsl = false, //for now
                Factory = factory,
            };

            idsrvApp.UseIdentityServer(options);
        });

        app.Map("/admin", adminApp =>
        {
            adminApp.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
            {
                Authority = "http://identity_local/core",
                IssuerName = "identity_local",
                ValidationMode = ValidationMode.Local,
                RequiredScopes = new[] { "api", "roles" }
            });

            adminApp.UseResourceAuthorization(new AuthorisationManager());

            var config = new HttpConfiguration();
            config.MapHttpAttributeRoutes();

            adminApp.UseCors(CorsOptions.AllowAll);
            adminApp.UseWebApi(config);

        });

Does anyone know if a) it is possible to have both in the same startup and b) if so, what have I done wrong or what can I do to remedy the above.

Contracted answered 12/6, 2016 at 22:11 Comment(0)
P
9

At startup time the UseIdentityServerBearerTokenAuthentication tries to contact the IdentityServer metatadata endpoint, but since the server is not yet running it can't connect, thus an error.

For this situation, there's a flag called DelayLoadMetadata to delay load the metadata until the first time it's needed: https://identityserver.github.io/Documentation/docsv2/consuming/options.html

Preachment answered 13/6, 2016 at 0:30 Comment(2)
You are my hero! I was searching for a solution for a week. And simply setting DelayLoadMetadata = true in the IdentityServerBearerTokenAuthenticationOptions did the trick. This should definitley be marked as the correct answer. Actually it should be in the getting started walkthrough by IdentityServer3.Luteal
Is this still an issue in IS4 if you know maybe? I have two apps restarting at almost same time on deploy API and IS4 and API totally loses it when IS4 is not available, like it caches sth from prior deployment of IS4 so I have to restart it again that it comes up after IS4.Darbydarce

© 2022 - 2024 — McMap. All rights reserved.