【问题标题】:UseWsFederationAuthentication - AuthenticationException: The remote certificate is invalid according to the validation procedureUseWsFederationAuthentication - AuthenticationException:根据验证程序,远程证书无效
【发布时间】:2017-02-05 15:31:30
【问题描述】:

每次我尝试通过 VS2015 和 IIS Express 在我的开发机器上运行我的 MVC 项目时都会遇到错误。它使用此代码对我们的 ADFS 服务器进行身份验证。

app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
            app.UseCookieAuthentication(new CookieAuthenticationOptions());

            app.UseWsFederationAuthentication(
                new WsFederationAuthenticationOptions
                {
                    Wtrealm = realm,
                    MetadataAddress = adfsMetadata
                });

这是我在浏览器中遇到的错误。

[AuthenticationException: 远程证书无效根据 到验证程序。]
System.Net.TlsStream.EndWrite(IAsyncResult asyncResult) +231
System.Net.PooledStream.EndWrite(IAsyncResult asyncResult) +15
System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar) +119

[WebException:底层连接已关闭:无法 为 SSL/TLS 安全通道建立信任关系。]
System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) +606 System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar) +64

[HttpRequestException: 发送请求时出错。]
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)+92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)+58 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +28 Microsoft.IdentityModel.Protocols.d__0.MoveNext() +453

[IOException:无法从以下位置获取文档: https://adfs.DOMAIN.com/FederationMetadata/2007-06/FederationMetadata.xml] Microsoft.IdentityModel.Protocols.d__0.MoveNext() +830 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)+92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)+58 System.Runtime.CompilerServices.TaskAwaiter1.GetResult() +28 Microsoft.IdentityModel.Protocols.<GetAsync>d__1.MoveNext() +606 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 System.Runtime.CompilerServices.TaskAwaiter1.GetResult() +28 Microsoft.IdentityModel.Protocols.d__3.MoveNext() +1332

[InvalidOperationException: IDX10803: 无法创建获取 配置来自: 'https://adfs.DOMAIN.com/FederationMetadata/2007-06/FederationMetadata.xml'。] Microsoft.IdentityModel.Protocols.d__3.MoveNext() +2226 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)+92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)+58 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +28 Microsoft.Owin.Security.WsFederation.d__c.MoveNext() +772 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)+92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)+58 System.Runtime.CompilerServices.TaskAwaiter.GetResult() +26 Microsoft.Owin.Security.Infrastructure.d__b.MoveNext() +447 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)+92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)+58 System.Runtime.CompilerServices.TaskAwaiter.GetResult() +26 Microsoft.Owin.Security.Infrastructure.d__8.MoveNext() +440 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)+92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)+58 System.Runtime.CompilerServices.TaskAwaiter.GetResult() +26 Microsoft.Owin.Security.Infrastructure.d__5.MoveNext() +266 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)+92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)+58 System.Runtime.CompilerServices.TaskAwaiter.GetResult() +26 Microsoft.Owin.Security.Infrastructure.d__0.MoveNext() +1174 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)+92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)+58 System.Runtime.CompilerServices.TaskAwaiter.GetResult() +26 Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.d__5.MoveNext() +287 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)+92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)+58 System.Runtime.CompilerServices.TaskAwaiter.GetResult() +26 Microsoft.Owin.Security.Infrastructure.d__0.MoveNext() +937 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)+92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)+58 System.Runtime.CompilerServices.TaskAwaiter.GetResult() +26 Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.d__5.MoveNext() +287 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)+92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)+58 System.Runtime.CompilerServices.TaskAwaiter.GetResult() +26 Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.d__2.MoveNext() +272 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +26 Microsoft.Owin.Host.SystemWeb.Infrastructure.ErrorState.Rethrow() +33 Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End(IAsyncResult ar) +150
Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.EndFinalWork(IAsyncResult ar) +42
System.Web.AsyncEventExecutionStep.OnAsyncEventCompletion(IAsyncResult a) +9791593

它可以工作,但现在它不再起作用了....为什么?

关于为什么这不起作用,为什么远程证书根据验证程序无效(该程序在哪里?),以及我如何使其恢复并运行的任何想法?

【问题讨论】:

    标签: asp.net-mvc ssl-certificate owin ws-federation adfs3.0


    【解决方案1】:

    再次感谢 Thuan 的回答。

    从答案中我能够思考到底发生了什么(这对于弄清楚事情总是至关重要的)。关键是意识到问题源于远程证书未通过验证检查,仅此而已。所以我发现在 UseWsFederationAuthentication 调用中有一个 BackchannelCertificateValidator 选项,可用于验证证书是否有效。然后我找到了这个post,它帮助我想出了验证证书的代码。

    帖子从 Microsoft 网站 (https://msdn.microsoft.com/en-us/library/office/dd633677(v=exchg.80).aspx) 找到了他们的代码。在文章中,它谈到了代码。

    本例中的证书验证回调方法提供 为 EWS 托管 API 的开发和测试提供足够的安全性 应用程序。但是,它可能无法为您提供足够的安全性 部署的应用程序。您应该始终确保证书 您使用的验证回调方法符合安全性 您的组织的要求。

    所以我必须想办法让它只在调试模式下运行。

    代码:

    public void ConfigureAuth(IAppBuilder app)
            {
    
                app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
                app.UseCookieAuthentication(new CookieAuthenticationOptions());
    
                app.UseWsFederationAuthentication(
                    new WsFederationAuthenticationOptions
                    {
    
                        BackchannelCertificateValidator = new CertificateValidator(),
    
                        Wtrealm = realm,
                        MetadataAddress = adfsMetadata
                    });
            }
    

    班级:

    public class CertificateValidator : ICertificateValidator
        {
            public bool Validate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
            {
                // If the certificate is a valid, signed certificate, return true.
                if (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None)
                {
                    return true;
                }
    
                // If there are errors in the certificate chain, look at each error to determine the cause.
                if ((sslPolicyErrors & System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors) != 0)
                {
                    if (chain != null && chain.ChainStatus != null)
                    {
                        foreach (System.Security.Cryptography.X509Certificates.X509ChainStatus status in chain.ChainStatus)
                        {
                            if ((certificate.Subject == certificate.Issuer) &&
                               (status.Status == System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.UntrustedRoot))
                            {
                                // Self-signed certificates with an untrusted root are valid. 
                                continue;
                            }
                            else
                            {
                                if (status.Status != System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.NoError)
                                {
                                    // If there are any other errors in the certificate chain, the certificate is invalid,
                                    // so the method returns false.
                                    return false;
                                }
                            }
                        }
                    }
    
                    // When processing reaches this line, the only errors in the certificate chain are 
                    // untrusted root errors for self-signed certificates. These certificates are valid
                    // for default Exchange server installations, so return true.
                    return true;
                }
                else
                {
                    // In all other cases, return false.
                    return false;
                }
            }
        }
    

    【讨论】:

    • 比简单地禁用开发环境验证更好的解决方案:)
    • 我尝试使用此方法在我的应用程序中的 aspnetcore2.1 中进行设置,但它给了我错误 SetDefaultSignInAsAuthenticationType not found 和 UseWsFederationAuthentication not found 错误。我是否缺少依赖项,我已经安装了Microsoft.Owin.SecurityMicrosoft.Owin.Security.CookiesMicrosoft.Owin.Security.WsFederation
    【解决方案2】:

    根据堆栈跟踪,身份验证过程的第一步是您的应用程序需要通过 https 下载 ADFS 的元数据,并且 SSL 证书未通过验证过程。

    关于验证程序是什么,您可以参考 SO 中的其他问题:The remote certificate is invalid according to the validation procedure

    可能有两种方法可以解决它:

    1. 黑客,不得用于生产是禁用检查:"The remote certificate is invalid according to the validation procedure." using Gmail SMTP server
    2. 如果 SSL 证书不是自签名的并且仍然有效(例如,由受信任的 CA 颁发、未过期且尚未撤销),您可能需要检查您的客户端计算机是否信任该 CA。这种情况比较少见。

    【讨论】:

    • 感谢 Thuan 的回答。看完你的答案后,我想出了一个答案。我稍后会发布。
    猜你喜欢
    • 2021-05-30
    • 2020-09-23
    • 2011-03-28
    • 2016-11-28
    • 2013-08-08
    • 2012-07-20
    • 2015-03-17
    相关资源
    最近更新 更多