private void HandleInit(Message message) { var status = this.serverContext.AcceptSecurityContext(message.Token, out byte[] nextToken); ContextDebugger.WriteLine($"[ServiceRequest] AcceptToken {status} | trip {tripCount} | next {nextToken?.Length ?? 0}"); tripCount++; if (status == ContextStatus.Accepted || status == ContextStatus.RequiresContinuation) { if (nextToken != null && nextToken.Length > 0) { this.SendResponse(new Message(Operation.ServerToken) { Token = nextToken }); } if (status == ContextStatus.Accepted) { ContextDebugger.WriteLine($"[ServiceRequest] context user {this.serverContext.UserName}"); var imp = this.serverContext.ImpersonateClient(); var identity = Thread.CurrentPrincipal.Identity as WindowsIdentity; ContextDebugger.WriteLine($"[ServiceRequest] impersonated {identity.Name} | {identity.ImpersonationLevel}"); } } }
private void Receive(byte[] readBuffer) { var message = Message.Deserialize(readBuffer); ContextDebugger.WriteLine($"[ServiceRequest] received {message.Operation}; s4u: {message.S4UToken}"); if (message.Operation == Operation.ClientToken) { HandleInit(message); } else { this.OnReceived?.Invoke(message); } }
private void SendResponse(Message message) { ContextDebugger.WriteLine($"[ServiceRequest] Send Response {message.Operation}"); if (cancel.IsCancellationRequested) { throw new InvalidOperationException("Not connected"); } var outBuffer = message.Serialize(); var lengthBuffer = new byte[4]; Endian.ConvertToBigEndian(outBuffer.Length, lengthBuffer); readSocket.Send(lengthBuffer, 0, lengthBuffer.Length, SocketFlags.None, out SocketError error); readSocket.Send(outBuffer, 0, outBuffer.Length, SocketFlags.None); }