Microsoft.SqlServer.TDS.EndPoint.SSPI.SecBufferDesc.ToArray C# (CSharp) Метод

ToArray() приватный Метод

Convert to byte array
private ToArray ( ) : byte[]
Результат byte[]
        internal byte[] ToArray()
        {
            // Check if we have a buffer
            if (BuffersPtr == IntPtr.Zero)
            {
                // We don'thave a buffer
                return null;
            }

            // Prepare a memory stream to contain all the buffers
            MemoryStream outputStream = new MemoryStream();

            // Iterate through each buffer and write the data into the stream
            for (int index = 0; index < BufferCount; index++)
            {
                // Calculate pointer to the buffer
                IntPtr currentBufferPtr = new IntPtr(BuffersPtr.ToInt64() + (index * Marshal.SizeOf(typeof(SecBuffer))));

                // Project the buffer into the managed world
                SecBuffer secBuffer = (SecBuffer)Marshal.PtrToStructure(currentBufferPtr, typeof(SecBuffer));

                // Get the byte buffer
                byte[] secBufferBytes = secBuffer.ToArray();

                // Write buffer to the stream
                outputStream.Write(secBufferBytes, 0, secBufferBytes.Length);
            }

            // Convert to byte array
            return outputStream.ToArray();
        }
    }

Usage Example

Пример #1
0
        /// <summary>
        /// Initialize authentication sequence for the client
        /// </summary>
        /// <param name="clientToken">Payload received from the server</param>
        /// <returns>Token to be sent to the server</returns>
        public SSPIResponse ContinueClientAuthentication(byte[] clientToken)
        {
            // Wrap client token with the security buffer
            SecBufferDesc serverSecBuffer = new SecBufferDesc(clientToken);

            try
            {
                // Allocate a new instance of the client security buffer of the specified size
                SecBufferDesc clientSecBuffer = new SecBufferDesc(_maxTokenBufferSize);

                try
                {
                    // Return code from the security API call
                    int secReturnCode = 0;

                    // New context attribute
                    uint contextAttribute = 0;

                    // Initialize token lifetime container
                    SecurityInteger lifeTime = new SecurityInteger();

                    // Delegate into security API
                    secReturnCode = SecurityWrapper.InitializeSecurityContext(ref _outboundCredential,
                                                                              ref _clientContext,
                                                                              _targetMachineSPN,
                                                                              (int)(SecContextRequirements.MutualAuthentication | SecContextRequirements.Delegate | SecContextRequirements.ExtendedError),
                                                                              0,
                                                                              (int)SecDataRepresentation.Native,
                                                                              ref serverSecBuffer,
                                                                              0,
                                                                              out _clientContext,
                                                                              out clientSecBuffer,
                                                                              out contextAttribute,
                                                                              out lifeTime);

                    // Check the return code
                    if (secReturnCode != (int)SecResult.Ok && secReturnCode != (int)SecResult.ContinueNeeded && secReturnCode != (int)SecResult.CompleteAndContinue)
                    {
                        // Operation failed
                        throw new Win32Exception(secReturnCode, "Failed to generate security context");
                    }

                    // NOTE: Digest SSP call to "CompleteAuthToken" is intentionally omitted because we don't support Digest today.

                    // Convert to byte array and indication whether this is a last call
                    return(new SSPIResponse(clientSecBuffer.ToArray(), secReturnCode != (int)SecResult.ContinueNeeded && secReturnCode != (int)SecResult.CompleteAndContinue));
                }
                finally
                {
                    // Dispose client security buffer
                    clientSecBuffer.Dispose();
                }
            }
            finally
            {
                // Dispose server security buffer
                serverSecBuffer.Dispose();
            }
        }
All Usage Examples Of Microsoft.SqlServer.TDS.EndPoint.SSPI.SecBufferDesc::ToArray