本文整理汇总了C#中Npgsql.NpgsqlConnector.Authenticate方法的典型用法代码示例。如果您正苦于以下问题:C# NpgsqlConnector.Authenticate方法的具体用法?C# NpgsqlConnector.Authenticate怎么用?C# NpgsqlConnector.Authenticate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Npgsql.NpgsqlConnector
的用法示例。
在下文中一共展示了NpgsqlConnector.Authenticate方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ProcessBackendResponses_Ver_3
protected IEnumerable<IServerResponseObject> ProcessBackendResponses_Ver_3(NpgsqlConnector context)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "ProcessBackendResponses");
using (new ContextResetter(context))
{
Stream stream = context.Stream;
NpgsqlMediator mediator = context.Mediator;
NpgsqlRowDescription lastRowDescription = null;
List<NpgsqlError> errors = new List<NpgsqlError>();
for (;;)
{
// Check the first Byte of response.
BackEndMessageCode message = (BackEndMessageCode) stream.ReadByte();
switch (message)
{
case BackEndMessageCode.ErrorResponse:
NpgsqlError error = new NpgsqlError(context.BackendProtocolVersion, stream);
error.ErrorSql = mediator.SqlSent;
errors.Add(error);
NpgsqlEventLog.LogMsg(resman, "Log_ErrorResponse", LogLevel.Debug, error.Message);
// Return imediately if it is in the startup state or connected state as
// there is no more messages to consume.
// Possible error in the NpgsqlStartupState:
// Invalid password.
// Possible error in the NpgsqlConnectedState:
// No pg_hba.conf configured.
if (!context.RequireReadyForQuery)
{
throw new NpgsqlException(errors);
}
break;
case BackEndMessageCode.AuthenticationRequest:
NpgsqlEventLog.LogMsg(resman, "Log_ProtocolMessage", LogLevel.Debug, "AuthenticationRequest");
// Get the length in case we're getting AuthenticationGSSContinue
int authDataLength = PGUtil.ReadInt32(stream) - 8;
AuthenticationRequestType authType = (AuthenticationRequestType) PGUtil.ReadInt32(stream);
switch (authType)
{
case AuthenticationRequestType.AuthenticationOk:
NpgsqlEventLog.LogMsg(resman, "Log_AuthenticationOK", LogLevel.Debug);
break;
case AuthenticationRequestType.AuthenticationClearTextPassword:
NpgsqlEventLog.LogMsg(resman, "Log_AuthenticationClearTextRequest", LogLevel.Debug);
// Send the PasswordPacket.
ChangeState(context, NpgsqlStartupState.Instance);
context.Authenticate(context.Password);
break;
case AuthenticationRequestType.AuthenticationMD5Password:
NpgsqlEventLog.LogMsg(resman, "Log_AuthenticationMD5Request", LogLevel.Debug);
// Now do the "MD5-Thing"
// for this the Password has to be:
// 1. md5-hashed with the username as salt
// 2. md5-hashed again with the salt we get from the backend
MD5 md5 = MD5.Create();
// 1.
byte[] passwd = context.Password;
byte[] saltUserName = ENCODING_UTF8.GetBytes(context.UserName);
byte[] crypt_buf = new byte[passwd.Length + saltUserName.Length];
passwd.CopyTo(crypt_buf, 0);
saltUserName.CopyTo(crypt_buf, passwd.Length);
StringBuilder sb = new StringBuilder();
byte[] hashResult = md5.ComputeHash(crypt_buf);
foreach (byte b in hashResult)
{
sb.Append(b.ToString("x2"));
}
String prehash = sb.ToString();
byte[] prehashbytes = ENCODING_UTF8.GetBytes(prehash);
crypt_buf = new byte[prehashbytes.Length + 4];
stream.Read(crypt_buf, prehashbytes.Length, 4);
// Send the PasswordPacket.
ChangeState(context, NpgsqlStartupState.Instance);
// 2.
prehashbytes.CopyTo(crypt_buf, 0);
//.........这里部分代码省略.........
示例2: ProcessBackendResponses_Ver_3
protected virtual void ProcessBackendResponses_Ver_3( NpgsqlConnector context )
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "ProcessBackendResponses");
Stream stream = context.Stream;
NpgsqlMediator mediator = context.Mediator;
// Often used buffers
Byte[] inputBuffer = new Byte[ 4 ];
String Str;
Boolean readyForQuery = false;
byte[] asciiRowBytes = new byte[300];
char[] asciiRowChars = new char[300];
while (!readyForQuery)
{
// Check the first Byte of response.
Int32 message = stream.ReadByte();
switch ( message )
{
case NpgsqlMessageTypes_Ver_3.ErrorResponse :
{
NpgsqlError error = new NpgsqlError(context.BackendProtocolVersion);
error.ReadFromStream(stream, context.Encoding);
error.ErrorSql = mediator.SqlSent;
mediator.Errors.Add(error);
NpgsqlEventLog.LogMsg(resman, "Log_ErrorResponse", LogLevel.Debug, error.Message);
}
// Return imediately if it is in the startup state or connected state as
// there is no more messages to consume.
// Possible error in the NpgsqlStartupState:
// Invalid password.
// Possible error in the NpgsqlConnectedState:
// No pg_hba.conf configured.
if (! mediator.RequireReadyForQuery)
{
return;
}
break;
case NpgsqlMessageTypes_Ver_3.AuthenticationRequest :
NpgsqlEventLog.LogMsg(resman, "Log_ProtocolMessage", LogLevel.Debug, "AuthenticationRequest");
// Eat length
PGUtil.ReadInt32(stream, inputBuffer);
{
Int32 authType = PGUtil.ReadInt32(stream, inputBuffer);
if ( authType == NpgsqlMessageTypes_Ver_3.AuthenticationOk )
{
NpgsqlEventLog.LogMsg(resman, "Log_AuthenticationOK", LogLevel.Debug);
break;
}
if ( authType == NpgsqlMessageTypes_Ver_3.AuthenticationClearTextPassword )
{
NpgsqlEventLog.LogMsg(resman, "Log_AuthenticationClearTextRequest", LogLevel.Debug);
// Send the PasswordPacket.
ChangeState( context, NpgsqlStartupState.Instance );
context.Authenticate(context.Password);
break;
}
if ( authType == NpgsqlMessageTypes_Ver_3.AuthenticationMD5Password )
{
NpgsqlEventLog.LogMsg(resman, "Log_AuthenticationMD5Request", LogLevel.Debug);
// Now do the "MD5-Thing"
// for this the Password has to be:
// 1. md5-hashed with the username as salt
// 2. md5-hashed again with the salt we get from the backend
MD5 md5 = MD5.Create();
// 1.
byte[] passwd = context.Encoding.GetBytes(context.Password);
byte[] saltUserName = context.Encoding.GetBytes(context.UserName);
byte[] crypt_buf = new byte[passwd.Length + saltUserName.Length];
passwd.CopyTo(crypt_buf, 0);
saltUserName.CopyTo(crypt_buf, passwd.Length);
//.........这里部分代码省略.........
示例3: ProcessBackendResponses_Ver_2
protected IEnumerable<IServerResponseObject> ProcessBackendResponses_Ver_2(NpgsqlConnector context)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "ProcessBackendResponses");
using (new ContextResetter(context))
{
Stream stream = context.Stream;
NpgsqlMediator mediator = context.Mediator;
NpgsqlRowDescription lastRowDescription = null;
List<NpgsqlError> errors = new List<NpgsqlError>();
for (;;)
{
// Check the first Byte of response.
switch ((BackEndMessageCode) stream.ReadByte())
{
case BackEndMessageCode.ErrorResponse:
{
NpgsqlError error = new NpgsqlError(context.BackendProtocolVersion, stream);
error.ErrorSql = mediator.SqlSent;
errors.Add(error);
NpgsqlEventLog.LogMsg(resman, "Log_ErrorResponse", LogLevel.Debug, error.Message);
}
// Return imediately if it is in the startup state or connected state as
// there is no more messages to consume.
// Possible error in the NpgsqlStartupState:
// Invalid password.
// Possible error in the NpgsqlConnectedState:
// No pg_hba.conf configured.
if (!context.RequireReadyForQuery)
{
throw new NpgsqlException(errors);
}
break;
case BackEndMessageCode.AuthenticationRequest:
NpgsqlEventLog.LogMsg(resman, "Log_ProtocolMessage", LogLevel.Debug, "AuthenticationRequest");
AuthenticationRequestType authType = (AuthenticationRequestType) PGUtil.ReadInt32(stream);
switch (authType)
{
case AuthenticationRequestType.AuthenticationOk:
NpgsqlEventLog.LogMsg(resman, "Log_AuthenticationOK", LogLevel.Debug);
break;
case AuthenticationRequestType.AuthenticationClearTextPassword:
NpgsqlEventLog.LogMsg(resman, "Log_AuthenticationClearTextRequest", LogLevel.Debug);
// Send the PasswordPacket.
ChangeState(context, NpgsqlStartupState.Instance);
context.Authenticate(context.Password);
break;
case AuthenticationRequestType.AuthenticationMD5Password:
NpgsqlEventLog.LogMsg(resman, "Log_AuthenticationMD5Request", LogLevel.Debug);
// Now do the "MD5-Thing"
// for this the Password has to be:
// 1. md5-hashed with the username as salt
// 2. md5-hashed again with the salt we get from the backend
MD5 md5 = MD5.Create();
// 1.
byte[] passwd = context.Password;
byte[] saltUserName = ENCODING_UTF8.GetBytes(context.UserName);
byte[] crypt_buf = new byte[passwd.Length + saltUserName.Length];
passwd.CopyTo(crypt_buf, 0);
saltUserName.CopyTo(crypt_buf, passwd.Length);
StringBuilder sb = new StringBuilder();
byte[] hashResult = md5.ComputeHash(crypt_buf);
foreach (byte b in hashResult)
{
sb.Append(b.ToString("x2"));
}
String prehash = sb.ToString();
byte[] prehashbytes = ENCODING_UTF8.GetBytes(prehash);
byte[] saltServer = new byte[4];
stream.Read(saltServer, 0, 4);
// Send the PasswordPacket.
ChangeState(context, NpgsqlStartupState.Instance);
// 2.
crypt_buf = new byte[prehashbytes.Length + saltServer.Length];
prehashbytes.CopyTo(crypt_buf, 0);
saltServer.CopyTo(crypt_buf, prehashbytes.Length);
sb = new StringBuilder("md5"); // This is needed as the backend expects md5 result starts with "md5"
hashResult = md5.ComputeHash(crypt_buf);
foreach (byte b in hashResult)
{
sb.Append(b.ToString("x2"));
//.........这里部分代码省略.........
示例4: ProcessBackendResponses_Ver_2
protected virtual void ProcessBackendResponses_Ver_2( NpgsqlConnector context )
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "ProcessBackendResponses");
Stream stream = context.Stream;
NpgsqlMediator mediator = context.Mediator;
// Often used buffer
Byte[] inputBuffer = new Byte[ 4 ];
Boolean readyForQuery = false;
byte[] asciiRowBytes = new byte[300];
char[] asciiRowChars = new char[300];
while (!readyForQuery)
{
// Check the first Byte of response.
switch ( stream.ReadByte() )
{
case NpgsqlMessageTypes_Ver_2.ErrorResponse :
{
NpgsqlError error = new NpgsqlError(context.BackendProtocolVersion);
error.ReadFromStream(stream, context.Encoding);
error.ErrorSql = mediator.SqlSent;
mediator.Errors.Add(error);
NpgsqlEventLog.LogMsg(resman, "Log_ErrorResponse", LogLevel.Debug, error.Message);
}
// Return imediately if it is in the startup state or connected state as
// there is no more messages to consume.
// Possible error in the NpgsqlStartupState:
// Invalid password.
// Possible error in the NpgsqlConnectedState:
// No pg_hba.conf configured.
if (! mediator.RequireReadyForQuery)
{
return;
}
break;
case NpgsqlMessageTypes_Ver_2.AuthenticationRequest :
NpgsqlEventLog.LogMsg(resman, "Log_ProtocolMessage", LogLevel.Debug, "AuthenticationRequest");
{
Int32 authType = PGUtil.ReadInt32(stream, inputBuffer);
if ( authType == NpgsqlMessageTypes_Ver_2.AuthenticationOk )
{
NpgsqlEventLog.LogMsg(resman, "Log_AuthenticationOK", LogLevel.Debug);
break;
}
if ( authType == NpgsqlMessageTypes_Ver_2.AuthenticationClearTextPassword )
{
NpgsqlEventLog.LogMsg(resman, "Log_AuthenticationClearTextRequest", LogLevel.Debug);
// Send the PasswordPacket.
ChangeState( context, NpgsqlStartupState.Instance );
context.Authenticate(context.Password);
break;
}
if ( authType == NpgsqlMessageTypes_Ver_2.AuthenticationMD5Password )
{
NpgsqlEventLog.LogMsg(resman, "Log_AuthenticationMD5Request", LogLevel.Debug);
// Now do the "MD5-Thing"
// for this the Password has to be:
// 1. md5-hashed with the username as salt
// 2. md5-hashed again with the salt we get from the backend
MD5 md5 = MD5.Create();
// 1.
byte[] passwd = context.Encoding.GetBytes(context.Password);
byte[] saltUserName = context.Encoding.GetBytes(context.UserName);
byte[] crypt_buf = new byte[passwd.Length + saltUserName.Length];
passwd.CopyTo(crypt_buf, 0);
saltUserName.CopyTo(crypt_buf, passwd.Length);
StringBuilder sb = new StringBuilder ();
byte[] hashResult = md5.ComputeHash(crypt_buf);
foreach (byte b in hashResult)
//.........这里部分代码省略.........