Exemple #1
0
        public byte[] Accept(byte[] token)
        {
            // TODO: A SecBufferDesc builder would be nice
            var incomingToken = new SecurityBufferDescription
            {
                Version = 0,
                Buffers = new[]
                {
                    new SecurityBuffer
                    {
                        Buffer     = token,
                        BufferType = SecurityBufferType.SECBUFFER_TOKEN
                    }
                }
            };

            var outgoingToken = new SecurityBufferDescription
            {
                Version = 0,
                Buffers = new[]
                {
                    new SecurityBuffer
                    {
                        // we need should query the sec package for them
                        Buffer     = new byte[64000],
                        BufferType = SecurityBufferType.SECBUFFER_TOKEN
                    }
                }
            };

            var result = SspiInterop.AcceptSecurityContext(
                ref _credentials,
                IntPtr.Zero,
                incomingToken,
                HTTP_SECURITY_ATTRIBUTES,
                SspiInterop.SECURITY_NETWORK_DREP,
                ref _context,
                outgoingToken,
                out var attributes,
                out var expiry);

            if (result == SspiInterop.SEC_E_OK || result == SspiInterop.SEC_I_COMPLETE_AND_CONTINUE)
            {
                IsEstablished = true;
                Principal     = GetPrincipalNameFromContext(_context);
                Roles         = GetGroupMembershipFromContext(_context);

                if (result == SspiInterop.SEC_E_OK)
                {
                    return(new byte[0]);
                }
            }
            if (result == SspiInterop.SEC_I_COMPLETE_AND_CONTINUE || result == SspiInterop.SEC_I_CONTINUE_NEEDED)
            {
                return(outgoingToken.Buffers
                       .FirstOrDefault(buffer => buffer.BufferType == SecurityBufferType.SECBUFFER_TOKEN)
                       .Buffer);
            }
            throw new AuthenticationException($"The SSPI Negotiate package was unable to accept the supplied authentication token (SSPI Status: {result})");
        }
Exemple #2
0
        public byte[] Initiate(byte[] token)
        {
            var outgoingToken = new SecurityBufferDescription
            {
                Version = 0,
                Buffers = new[]
                {
                    new SecurityBuffer
                    {
                        Buffer = new byte[64000],
                        BufferType = SecurityBufferType.SECBUFFER_TOKEN
                    }
                }
            };

            //var att = SspiInterop.ISC_REQ_USE_SUPPLIED_CREDS;
            var result = SspiInterop.InitializeSecurityContext(
                ref _credentials,
                IntPtr.Zero,
                _target,
                0,
                0,
                0,
                IntPtr.Zero, 
                0,
                ref _context,
                outgoingToken,
                out var attribute,
                out var expiry);

            if (result != 0)
            {
                Console.WriteLine($"InitializeSecurityContext returned {result}");
                Console.WriteLine(BitConverter.ToString(outgoingToken.Buffers[0].Buffer));
            }

            return outgoingToken.Buffers
                .FirstOrDefault(buffer => buffer.BufferType == SecurityBufferType.SECBUFFER_TOKEN)
                .Buffer;
        }