Marlon Brando aka Ben
Marlon Brando aka Ben

Reputation: 933

An unhandled exception has occurred: Malformed URL in Release mode in .net core 2.0 app

I have a .net core 2.0 app which uses IdentityServer 4. it works perfectly in development mode. then I published it for production mode and tested. when I click a (that action has a method to generated accesstoken) link I got an error like below,

An unhandled exception has occurred: Malformed URL

then error occurs here in production(Release) mode:

var disco = await IdentityModel.Client.DiscoveryClient.GetAsync(_configuration.GetSection("Settings").GetSection("DiscoveryClient").Value);

above DiscoveryClient is http not https here is the full description of the error ..

Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[0]
      An unhandled exception has occurred: Malformed URL
System.InvalidOperationException: Malformed URL
   at IdentityModel.Client.DiscoveryClient.ParseUrl(String input)
   at IdentityModel.Client.DiscoveryClient..ctor(String authority, HttpMessageHandler innerHandler)
   at IdentityModel.Client.DiscoveryClient.<GetAsync>d__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 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()

is this because 'https'. I have no idea what is happening.hope your help with this.

Upvotes: 1

Views: 2231

Answers (2)

Andrija Karadžić
Andrija Karadžić

Reputation: 61

This is a pretty old post, but it still might help someone. I had this exact same issue, only that the URL that I was passing in was perfectly fine. The problem on my side was that the IdentityServer Authority URL was misconfigured, causing it to throw the same exception.

Upvotes: 1

Richard
Richard

Reputation: 1584

Here is code snippet from DiscoveryClient's ParseUrl showing when it throws that exception:

public static DiscoveryEndpoint ParseUrl(string input)
{
    var success = Uri.TryCreate(input, UriKind.Absolute, out var uri);
    if (success == false)
    {
        throw new InvalidOperationException("Malformed URL");
    }

    if (!DiscoveryEndpoint.IsValidScheme(uri))
    {
        throw new InvalidOperationException("Malformed URL");
    }

Here is the code for DiscoveryEndpoint's IsValidScheme method:

public static bool IsValidScheme(Uri url)
{
    if (string.Equals(url.Scheme, "http", StringComparison.OrdinalIgnoreCase) ||
        string.Equals(url.Scheme, "https", StringComparison.OrdinalIgnoreCase))
    {
        return true;
    }

    return false;
}

Based on this the exception is not thrown because the url is using http.

Try calling

new Uri(_configuration.GetSection("Settings").GetSection("DiscoveryClient").Value, UriKind.Absolute)

before your call to 'IdentityModel.Client.DiscoveryClient.GetAsync' so that you can see what exception is thrown by the Uri constructor.

Upvotes: 0

Related Questions