【问题标题】:An unhandled exception has occurred: Malformed URL in Release mode in .net core 2.0 app发生未处理的异常:.net core 2.0 应用程序中发布模式中的 URL 格式错误
【发布时间】:2018-06-13 07:35:11
【问题描述】:

我有一个使用 IdentityServer 4.net core 2.0 应用程序。它在开发模式下完美运行。然后我将其发布为生产模式并进行了测试。当我单击一个(该操作具有生成 accesstoken 的方法)链接时,出现如下错误,

发生未处理的异常:URL 格式错误

然后在production(Release)模式下出现错误:

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

DiscoveryClient 以上是 http 而不是 https 这是错误的完整描述..

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()

这是因为'https'。我不知道发生了什么。希望您对此有所帮助。

【问题讨论】:

    标签: release identityserver4 .net-core-2.0 malformedurlexception


    【解决方案1】:

    这是来自DiscoveryClientParseUrl 的代码 sn-p,显示了它何时抛出该异常:

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

    这是DiscoveryEndpointIsValidScheme方法的代码:

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

    基于此,不会抛出异常,因为 url 使用的是http

    尝试调用

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

    在调用“IdentityModel.Client.DiscoveryClient.GetAsync”之前,您可以查看 Uri 构造函数引发的异常。

    【讨论】:

      【解决方案2】:

      这是一篇相当老的帖子,但它仍然可能对某人有所帮助。我遇到了同样的问题,只是我传入的 URL 非常好。我这边的问题是 IdentityServer Authority URL 配置错误,导致它抛出相同的异常。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-25
        • 1970-01-01
        • 2014-12-15
        • 2011-05-27
        • 1970-01-01
        相关资源
        最近更新 更多