IdentityServer4 icon indicating copy to clipboard operation
IdentityServer4 copied to clipboard

API can't access .well-known/openid-configuration but I can from my browser

Open paetobuddy opened this issue 9 years ago • 14 comments

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

Issue / Steps to reproduce the problem

My API can't access the discovery link on my IdentityServer, yet when I open it in browser it works - https://localhost:44318/.well-known/openid-configuration

Code in my API

 IdentityServerAuthenticationOptions identityServerAuthenticationOptions = new IdentityServerAuthenticationOptions()
            {
                Authority = "https://localhost:44318/",
                AllowedScopes = new List<string> { "securedFiles" },
                ApiSecret = "securedFilesSecret",
                ApiName = "securedFiles",
                AutomaticAuthenticate = true,
                // required if you want to return a 403 and not a 401 for forbidden responses
                AutomaticChallenge = true
            };
            JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
app.UseIdentityServerAuthentication(identityServerAuthenticationOptions);

I can reproduce same error on this repository: https://github.com/damienbod/AspNet5IdentityServerAngularImplicitFlow/ with its IdentityServerWithIdentitySQLite and ResourceFileServer API.

Also, when I try to access the URL via GET request - https://localhost:44318/.well-known/openid-configuration from Console app I get almost the same error.

System.InvalidOperationException: Discovery endpoint https://localhost:44318/.well-known/openid-configuration is unavailable: System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.Http.WinHttpException: A security error occurred

However, following code works:

Code that works

 using (var httpClientHandler = new HttpClientHandler())
            {

                httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) =>
                {
                    return true;
                };
                using (var client = new HttpClient(httpClientHandler))
                {
                    try
                    {
                        var response = await client.GetAsync(“https://localhost:44318/.well-known/openid-configuration”);
                        return response;
                    }
                    catch (HttpRequestException ex)
                    {
                        var c = ex;
                        throw;
                    }
                }
            }

Relevant parts of the log file

System.InvalidOperationException: Discovery endpoint https://localhost:44318/.well-known/openid-configuration is unavailable: System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.Http.WinHttpException: A security error occurred
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Net.Http.WinHttpHandler.<StartRequest>d__105.MoveNext()
   --- End of inner exception stack trace ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Net.Http.HttpClient.<FinishSendAsync>d__58.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Net.Http.HttpClient.<GetContentAsync>d__32`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at IdentityModel.AspNetCore.OAuth2Introspection.OAuth2IntrospectionMiddleware.<GetIntrospectionEndpointFromDiscoveryDocument>d__6.MoveNext()
   at IdentityModel.AspNetCore.OAuth2Introspection.OAuth2IntrospectionMiddleware.<GetIntrospectionEndpointFromDiscoveryDocument>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at IdentityModel.AspNetCore.OAuth2Introspection.OAuth2IntrospectionMiddleware.<InitializeIntrospectionClient>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at IdentityModel.AspNetCore.OAuth2Introspection.OAuth2IntrospectionHandler.<LoadClaimsForToken>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at IdentityModel.AspNetCore.OAuth2Introspection.OAuth2IntrospectionHandler.<HandleAuthenticateAsync>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Authentication.AuthenticationHandler`1.<InitializeAsync>d__52.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware`1.<Invoke>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at IdentityServer4.AccessTokenValidation.IdentityServerAuthenticationMiddleware.<Invoke>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware.<Invoke>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Server.IISIntegration.IISMiddleware.<Invoke>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Hosting.Internal.RequestServicesContainerMiddleware.<Invoke>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Frame`1.<RequestProcessingAsync>d__2.MoveNext()
2017-04-29T14:27:35.0584661+02:00 0HL4EVBG94FNN [INF] Request finished in 158.8986ms 500  (15c52c40)

Any idea how I can make this work, at least for DEV environment ?

paetobuddy avatar Apr 29 '17 12:04 paetobuddy

Well - in your "test code" you disable HTTPS validation.

httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };

So it is most probably a problem with your server/certificate configuration.

leastprivilege avatar Apr 29 '17 14:04 leastprivilege

Yeah, I'm aware of that but I do not know what can be an issue. I created a certificate, installed it to my store. Tried using a certificate from that damienbod's repo and install it to my machine, again the same issue. I am clueless, there is nothing useful in system/application logs.

What could I do to inspect this issue further so I can hopefully get closer to solution. Any tips ?

paetobuddy avatar Apr 29 '17 14:04 paetobuddy

look at the exact error in your test code.

leastprivilege avatar Apr 29 '17 14:04 leastprivilege

I did. I get the following for the exception message: {"The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel."} -> "The remote certificate is invalid according to the validation procedure."

Tried solutions from StackOverflow and other sites, did not find anything useful or something that helped. Should I somehow connect the certificate that is used by my IdentityServer project with IIS Express ?

paetobuddy avatar Apr 29 '17 14:04 paetobuddy

{"The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel."} -> "The remote certificate is invalid according to the validation procedure."

This is your exact error. It means your machine does not trust the SSL cert.

brockallen avatar Apr 29 '17 21:04 brockallen

A couple things to try...

  • Make sure the account running the IdS site has permission to access the certificate, go to Certificate Management, right click certificate > All Tasks > Manage Private Keys and give the account Full Control over the certificate
  • Use a different certificate for IdS encryption/signatures than for the site's SSL/TLS traffic
  • Ensure the entire chain of certificate are valid and the root is indeed Trusted

travisjs avatar May 02 '17 20:05 travisjs

Anyone knows how to deal with that trust certificate errors on Service Fabric? Getting the same errors. My IdentityServer4 and WebApi are both in ServiceFabric

neil445 avatar May 16 '17 16:05 neil445

Any update on the issue? closing for now - feel free to re-open if it needs further discussion.

leastprivilege avatar May 22 '17 04:05 leastprivilege

Hi, I had this issue for over a week and this is how I resolved it:

The problem I had was the fact that I was using the "IIS Express Development Certificate" and for some odd reason it wasn't a trusted certificate on my dev machine hence it was only broken on this particular environment. To fix it, export the IIS Express Development Certificate and import it back in to the "Trusted Root Certification Authorities"->"Certificate".

Farshidgolkarihagh avatar Aug 15 '17 13:08 Farshidgolkarihagh

I had this issue, after importing the certificate into trusted root certificate place issue got resolved.

SathishManokar avatar Dec 13 '18 05:12 SathishManokar

The problem I had was the fact that I was using the "IIS Express Development Certificate" and for some odd reason it wasn't a trusted certificate on my dev machine hence it was only broken on this particular environment. To fix it, export the IIS Express Development Certificate and import it back in to the "Trusted Root Certification Authorities"->"Certificate".

I realize this is over a year old, but I'm having the same problem as described. The only difference is we're behind a reverse proxy. This is by no means a release, it's just for QA purposes. How can I export the cert from iis and import to trusted? I'm not really a network guy. If anyone can point me to a tutorial that would be fantastic!! Thanks!!!

LNShimmell avatar Dec 18 '18 17:12 LNShimmell

bump

The problem I had was the fact that I was using the "IIS Express Development Certificate" and for some odd reason it wasn't a trusted certificate on my dev machine hence it was only broken on this particular environment. To fix it, export the IIS Express Development Certificate and import it back in to the "Trusted Root Certification Authorities"->"Certificate".

I realize this is over a year old, but I'm having the same problem as described. The only difference is we're behind a reverse proxy. This is by no means a release, it's just for QA purposes. How can I export the cert from iis and import to trusted? I'm not really a network guy. If anyone can point me to a tutorial that would be fantastic!! Thanks!!!

bump

morrisond91 avatar Jul 09 '19 12:07 morrisond91

It may be some configuration error in your projects. Suppose you have three projects, Auth, Web Api, Spa. You need to make sure all configuration files of them have been used same IP(Web server public IP) instead of 'localhost'. Since it cannot work in remote web server like IIS by using localhost. The configuration files like launchSetting.js, Startup.cs, Program.cs, Config.cs, and TS files for Auth, Web Api share baseurl file, etc. Besides, you might add Url Rewrite to make a Url rule for Spa like Angular in some web server like IIS.

mcabram avatar Sep 03 '19 08:09 mcabram

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 10 '20 20:01 lock[bot]