public bool TryAcquireServerChallenge(WindowsAuthenticationToken message)
        {
            bool result = false;

            SecurityBufferDesciption clientToken = new SecurityBufferDesciption(message.Data);
            SecurityBufferDesciption serverToken = new SecurityBufferDesciption(MaximumTokenSize);

            try
            {
                SecurityInteger lifetime = new SecurityInteger(0);
                uint contextAttributes;

                if (NativeMethods.AcquireCredentialsHandle(null, "NTLM", SecurityCredentialsInbound, IntPtr.Zero, IntPtr.Zero, 0, IntPtr.Zero, ref credentials, ref lifetime) == 0)
                {
                    if (NativeMethods.AcceptSecurityContext(ref credentials, IntPtr.Zero, ref clientToken, StandardContextAttributes, SecurityNativeDataRepresentation, out context, out serverToken, out contextAttributes, out lifetime) == IntermediateResult)
                    {
                        result = true;
                    }
                }
            }
            finally
            {
                message.Data = serverToken.GetBytes();
                clientToken.Dispose();
                serverToken.Dispose();
            }

            return result;
        }
        public bool IsClientResponseValid(WindowsAuthenticationToken token)
        {
            bool result = false;

            SecurityBufferDesciption clientToken = new SecurityBufferDesciption(token.Data);
            SecurityBufferDesciption serverToken = new SecurityBufferDesciption(MaximumTokenSize);
            IntPtr securityContextHandle = IntPtr.Zero;

            try
            {
                uint contextAttributes;
                var lifetime = new SecurityInteger(0);

                if (NativeMethods.AcceptSecurityContext(ref credentials, ref context, ref clientToken, StandardContextAttributes, SecurityNativeDataRepresentation, out context, out serverToken, out contextAttributes, out lifetime) == 0)
                {
                    if (NativeMethods.QuerySecurityContextToken(ref context, ref securityContextHandle) == 0)
                    {
                        using (WindowsIdentity identity = new WindowsIdentity(securityContextHandle))
                        { 
                            if (identity != null)
                            {
                                AuthenticatedUsername = identity.Name;
                                result = true;
                            }
                        }
                    }
                }
            }
            finally
            {
                clientToken.Dispose();
                serverToken.Dispose();
                NativeMethods.CloseHandle(securityContextHandle);
                credentials.Reset();
                context.Reset();
            }

            return result;
        }
Exemplo n.º 3
0
 public static extern int AcceptSecurityContext(ref SecurityHandle phCredential,
     ref SecurityHandle phContext,
     ref SecurityBufferDesciption pInput,
     uint fContextReq,
     uint TargetDataRep,
     out SecurityHandle phNewContext,
     out SecurityBufferDesciption pOutput,
     out uint pfContextAttr,
     out SecurityInteger ptsTimeStamp
     );