当前位置: 首页>>代码示例>>C#>>正文


C# NpgsqlConnector.DefaultValidateRemoteCertificateCallback方法代码示例

本文整理汇总了C#中Npgsql.NpgsqlConnector.DefaultValidateRemoteCertificateCallback方法的典型用法代码示例。如果您正苦于以下问题:C# NpgsqlConnector.DefaultValidateRemoteCertificateCallback方法的具体用法?C# NpgsqlConnector.DefaultValidateRemoteCertificateCallback怎么用?C# NpgsqlConnector.DefaultValidateRemoteCertificateCallback使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Npgsql.NpgsqlConnector的用法示例。


在下文中一共展示了NpgsqlConnector.DefaultValidateRemoteCertificateCallback方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Open


//.........这里部分代码省略.........

                    try
                    {
                        IAsyncResult result = socket.BeginConnect(ep, null, null);

                        if (!result.AsyncWaitHandle.WaitOne(context.ConnectionTimeout*1000, true))
                        {
                            socket.Close();
                            throw new Exception(resman.GetString("Exception_ConnectionTimeout"));
                        }

                        socket.EndConnect(result);

                        // connect was successful, leave the loop
                        break;
                    }
                    catch (Exception)
                    {
                        NpgsqlEventLog.LogMsg(resman, "Log_FailedConnection", LogLevel.Normal, ip);
                        socket.Close();
                    }
                }

                if (socket == null || !socket.Connected)
                {
                    throw new Exception(string.Format(resman.GetString("Exception_FailedConnection"), context.Host));
                }

                //Stream stream = new NetworkStream(socket, true);
                Stream stream = new NpgsqlNetworkStream(context, socket, true);

                // If the PostgreSQL server has SSL connectors enabled Open SslClientStream if (response == 'S') {
                if (context.SSL || (context.SslMode == SslMode.Require) || (context.SslMode == SslMode.Prefer))
                {
                    PGUtil.WriteInt32(stream, 8);
                    PGUtil.WriteInt32(stream, 80877103);
                    // Receive response

                    Char response = (Char) stream.ReadByte();
                    if (response == 'S')
                    {
                        //create empty collection
                        X509CertificateCollection clientCertificates = new X509CertificateCollection();

                        //trigger the callback to fetch some certificates
                        context.DefaultProvideClientCertificatesCallback(clientCertificates);

                        if (context.UseMonoSsl)
                        {
                            stream = new SslClientStream(
                                stream,
                                context.Host,
                                true,
                                SecurityProtocolType.Default,
                                clientCertificates);

                            ((SslClientStream)stream).ClientCertSelectionDelegate =
                                new CertificateSelectionCallback(context.DefaultCertificateSelectionCallback);
                            ((SslClientStream)stream).ServerCertValidationDelegate =
                                new CertificateValidationCallback(context.DefaultCertificateValidationCallback);
                            ((SslClientStream)stream).PrivateKeyCertSelectionDelegate =
                                new PrivateKeySelectionCallback(context.DefaultPrivateKeySelectionCallback);
                        }
                        else
                        {
                            SslStream sstream = new SslStream(stream, true, delegate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors errors)
                            {
                                return context.DefaultValidateRemoteCertificateCallback(cert, chain, errors);
                            });
                            sstream.AuthenticateAsClient(context.Host, clientCertificates, System.Security.Authentication.SslProtocols.Default, false);
                            stream = sstream;
                        }
                    }
                    else if (context.SslMode == SslMode.Require)
                    {
                        throw new InvalidOperationException(resman.GetString("Exception_Ssl_RequestError"));
                    }
                }

                context.Stream = new BufferedStream(stream);
                context.Socket = socket;

                NpgsqlEventLog.LogMsg(resman, "Log_ConnectedTo", LogLevel.Normal, context.Host, context.Port);
                ChangeState(context, NpgsqlConnectedState.Instance);
            }
                //FIXME: Exceptions that come from what we are handling should be wrapped - e.g. an error connecting to
                //the server should definitely be presented to the uesr as an NpgsqlError. Exceptions from userland should
                //be passed untouched - e.g. ThreadAbortException because the user started this in a thread they created and
                //then aborted should be passed through.
                //Are there any others that should be pass through? Alternatively, are there a finite number that should
                //be wrapped?
            catch (ThreadAbortException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new NpgsqlException(e.Message, e);
            }
        }
开发者ID:jasonabi,项目名称:Npgsql2,代码行数:101,代码来源:NpgsqlClosedState.cs

示例2: Open


//.........这里部分代码省略.........
                    NpgsqlEventLog.LogMsg(resman, "Log_ConnectingTo", LogLevel.Debug, ips[i]);

                    IPEndPoint ep = new IPEndPoint(ips[i], context.Port);
                    socket = new Socket(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

                    attemptStart = DateTime.Now;

                    try
                    {
                        result = socket.BeginConnect(ep, null, null);

                        if (!result.AsyncWaitHandle.WaitOne(timeout / (ips.Length - i), true))
                        {
                            throw new TimeoutException(resman.GetString("Exception_ConnectionTimeout"));
                        }

                        socket.EndConnect(result);

                        // connect was successful, leave the loop
                        break;
                    }
                    catch (Exception e)
                    {
                        NpgsqlEventLog.LogMsg(resman, "Log_FailedConnection", LogLevel.Normal, ips[i]);

                        timeout -= Convert.ToInt32((DateTime.Now - attemptStart).TotalMilliseconds);
                        lastSocketException = e;

                        socket.Close();
                        socket = null;
                    }
                }

                if (socket == null)
                {
                    throw lastSocketException;
                }

                //Stream stream = new NetworkStream(socket, true);
                                Stream stream = new NpgsqlNetworkStream(context, socket, true);

                // If the PostgreSQL server has SSL connectors enabled Open SslClientStream if (response == 'S') {
                if (context.SSL || (context.SslMode == SslMode.Require) || (context.SslMode == SslMode.Prefer))
                {
                    PGUtil.WriteInt32(stream, 8);
                    PGUtil.WriteInt32(stream, 80877103);
                    // Receive response

                    Char response = (Char) stream.ReadByte();
                    if (response == 'S')
                    {
                                                //create empty collection
                                                X509CertificateCollection clientCertificates = new X509CertificateCollection();

                                                //trigger the callback to fetch some certificates
                                                context.DefaultProvideClientCertificatesCallback(clientCertificates);

                                                if (context.UseMonoSsl)
                                                {
                                                        stream = new SslClientStream(
                                                                stream,
                                                                context.Host,
                                                                true,
                                                                SecurityProtocolType.Default,
                                                                clientCertificates);

                                                        ((SslClientStream)stream).ClientCertSelectionDelegate =
                                                                new CertificateSelectionCallback(context.DefaultCertificateSelectionCallback);
                                                        ((SslClientStream)stream).ServerCertValidationDelegate =
                                                                new CertificateValidationCallback(context.DefaultCertificateValidationCallback);
                                                        ((SslClientStream)stream).PrivateKeyCertSelectionDelegate =
                                                                new PrivateKeySelectionCallback(context.DefaultPrivateKeySelectionCallback);
                                                }
                                                else
                                                {
                                                        SslStream sstream = new SslStream(stream, true, delegate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors errors)
                                                        {
                                                                return context.DefaultValidateRemoteCertificateCallback(cert, chain, errors);
                                                        });
                                                        sstream.AuthenticateAsClient(context.Host, clientCertificates, System.Security.Authentication.SslProtocols.Default, false);
                                                        stream = sstream;
                                                }
                    }
                    else if (context.SslMode == SslMode.Require)
                    {
                        throw new InvalidOperationException(resman.GetString("Exception_Ssl_RequestError"));
                    }
                }

                context.Stream = new BufferedStream(stream);
                context.Socket = socket;

                NpgsqlEventLog.LogMsg(resman, "Log_ConnectedTo", LogLevel.Normal, context.Host, context.Port);
                ChangeState(context, NpgsqlConnectedState.Instance);
            }
            catch (Exception e)
            {
                throw new NpgsqlException(string.Format(resman.GetString("Exception_FailedConnection"), context.Host), e);
            }
        }
开发者ID:Rungee,项目名称:Npgsql2,代码行数:101,代码来源:NpgsqlClosedState.cs


注:本文中的Npgsql.NpgsqlConnector.DefaultValidateRemoteCertificateCallback方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。