/***************************************************************************** * Function : GenerateToken * Description : This function generate token using SessionOptions.AuthenticationOptions * information. Returns false on failure. * Arguments : reference token string * Returns : bool *****************************************************************************/ private bool GenerateToken(out string token) { bool isTokenSuccess = false; bool isRunning = false; token = string.Empty; CorrelationID tokenReqId = new CorrelationID(99); EventQueue tokenEventQueue = new EventQueue(); d_session.GenerateToken(tokenReqId, tokenEventQueue); while (!isRunning) { Event eventObj = tokenEventQueue.NextEvent(); if (eventObj.Type == Event.EventType.TOKEN_STATUS) { System.Console.WriteLine("processTokenEvents"); foreach (Message msg in eventObj) { System.Console.WriteLine(msg.ToString()); if (msg.MessageType == TOKEN_SUCCESS) { token = msg.GetElementAsString("token"); isTokenSuccess = true; isRunning = true; break; } else if (msg.MessageType == TOKEN_FAILURE) { System.Console.WriteLine("Received : " + TOKEN_FAILURE.ToString()); isRunning = true; break; } else { System.Console.WriteLine("Error while Token Generation"); isRunning = true; break; } } } } return(isTokenSuccess); }
/// <summary> /// Authorize should be called before any requests are sent. Called by init. /// </summary> /// <param name="identity"></param> /// <param name="session"></param> private static void Authorize(out Identity identity, Session session) { identity = session.CreateIdentity(); if (!session.OpenService(AUTH_SERVICE)) { throw new Exception( string.Format("Failed to open auth service: {0}", AUTH_SERVICE)); } Service authService = session.GetService(AUTH_SERVICE); EventQueue tokenEventQueue = new EventQueue(); session.GenerateToken(new CorrelationID(tokenEventQueue), tokenEventQueue); string token = null; // Generate token responses will come on the dedicated queue. There would be no other // messages on that queue. Event eventObj = tokenEventQueue.NextEvent( Convert.ToInt32(WAIT_TIME.TotalMilliseconds)); if (eventObj.Type == Event.EventType.TOKEN_STATUS || eventObj.Type == Event.EventType.REQUEST_STATUS) { foreach (Message msg in eventObj) { System.Console.WriteLine(msg); if (msg.MessageType == TOKEN_SUCCESS) { token = msg.GetElementAsString(TOKEN_ELEMENT); } } } if (token == null) { throw new Exception("Failed to get token"); } Request authRequest = authService.CreateAuthorizationRequest(); authRequest.Set(TOKEN_ELEMENT, token); session.SendAuthorizationRequest(authRequest, identity, null); TimeSpan ts = WAIT_TIME; for (DateTime startTime = DateTime.UtcNow; ts.TotalMilliseconds > 0; ts = ts - (DateTime.UtcNow - startTime)) { eventObj = session.NextEvent(Convert.ToInt32(ts.TotalMilliseconds)); // Since no other requests were sent using the session queue, the response can // only be for the Authorization request if (eventObj.Type != Event.EventType.RESPONSE && eventObj.Type != Event.EventType.PARTIAL_RESPONSE && eventObj.Type != Event.EventType.REQUEST_STATUS) { continue; } foreach (Message msg in eventObj) { System.Console.WriteLine(msg); if (msg.MessageType != AUTHORIZATION_SUCCESS) { throw new Exception("Authorization Failed"); } } return; } throw new Exception("Authorization Failed"); }