/// <summary> /// Tring to recive partial authorized token from target server. /// </summary> /// <param name="cancellationToken">Token that can be used to termination of the logon process.</param> /// <returns></returns> public bool TryToGetGuestToken(CancellationToken cancellationToken) { bool asyncOperationStarted = false; #region Guest token processing // Is the guest token is relevant. bool guestTokenInvalid = string.IsNullOrEmpty(GuestTokenHandler.Token) || Tokens.IsExpired(GuestTokenHandler.Token, GuestTokenHandler.ExpiryTime); if (guestTokenInvalid) { // Lock thread. asyncOperationStarted = true; // Callback that will be call when guest token would be recived. GuestTokenHandler.ProcessingFinished += GuestTokenRecivedCallback; void GuestTokenRecivedCallback(QueryProcessor _, bool result, object message) { // Unsubscribe from handler. GuestTokenHandler.ProcessingFinished -= GuestTokenRecivedCallback; // Unlock thread. asyncOperationStarted = false; } // Recive guest token to get access to server. GuestTokenHandler.TryToReciveTokenAsync( routingIP, guestChanel, cancellationToken); } // Wait for guest token. while (asyncOperationStarted) { Thread.Sleep(50); } // Drop if guest token not recived. if (string.IsNullOrEmpty(GuestTokenHandler.Token)) { Console.WriteLine(routingIP + "/" + pipeName + ": GUEST TOKEN NOT RECEIVED"); return(false); } #endregion Console.WriteLine(routingIP + "/" + pipeName + ": GUEST TOKEN RECEIVED: " + GuestTokenHandler.Token); return(true); }
/// <summary> /// Tring to recive token authorized in authority controller of target server. /// </summary> /// <param name="cancellationToken">Token that can be used to termination of the logon process.</param> /// <returns></returns> public bool TryToLogon(CancellationToken cancellationToken) { // Drop if already started. if (GuestTokenHandler.IsInProgress) { return(false); } bool asyncOperationStarted = false; #region Guest token processing // Is the guest token is relevant. bool guestTokenInvalid = string.IsNullOrEmpty(GuestTokenHandler.Token) || Tokens.IsExpired(GuestTokenHandler.Token, GuestTokenHandler.ExpiryTime); if (guestTokenInvalid) { // Lock thread. asyncOperationStarted = true; // Callback that will be call when guest token would be recived. GuestTokenHandler.ProcessingFinished += GuestTokenRecivedCallback; void GuestTokenRecivedCallback(QueryProcessor _, bool result, object message) { // Unsubscribe from handler. GuestTokenHandler.ProcessingFinished -= GuestTokenRecivedCallback; // Unlock thread. asyncOperationStarted = false; } // Recive guest token to get access to server. GuestTokenHandler.TryToReciveTokenAsync( routingIP, pipeName, cancellationToken); } // Wait for guest token. while (asyncOperationStarted) { Thread.Sleep(100); } // Drop if guest token not recived. if (string.IsNullOrEmpty(GuestTokenHandler.Token)) { return(false); } #endregion #region Logon processing // Lock thread. asyncOperationStarted = true; // Callback that will be call whenlogon would be finished. LogonHandler.ProcessingFinished += LogonFinishedCallback; void LogonFinishedCallback(QueryProcessor _, bool result, object message) { // Unsubscribe from handler. LogonHandler.ProcessingFinished -= LogonFinishedCallback; // Unlock thread. asyncOperationStarted = false; } // Request logon. LogonHandler.TryToLogonAsync( GuestTokenHandler.Token, authLogin, authPassword, routingIP, pipeName); // Wait for guest token. while (asyncOperationStarted) { Thread.Sleep(100); } // Drop if guest token not recived. if (string.IsNullOrEmpty(LogonHandler.Token)) { return(false); } #endregion return(true); }