Example #1
0
        /// <summary>
        /// Processes logon.
        /// </summary>
        /// <param name="sessionID">Unique session key (created on client side)</param>
        /// <param name="credentials">Logon credentials</param>
        public void Logon(Guid sessionID, Hashtable credentials)
        {
            if (sessionID == Guid.Empty)
            {
                throw new ArgumentException(LanguageResource.ArgumentException_EmptySessionIDIsNotAllowed, "sessionID");
            }

            // prepare client IP address
            var clientAddress = GetCallingClientIPAddress();

            try
            {
                if (!_host.SessionManager.ExistSession(sessionID))
                {
                    // reset current session before authentication is complete
                    _host.SessionManager.SetCurrentSession(null);

                    // authenticate
                    var authResponse = _host.Authenticate(new AuthRequestMessage
                    {
                        Credentials   = credentials,
                        ClientAddress = clientAddress
                    });

                    if (!authResponse.Success)
                    {
                        var exception = authResponse.Exception ?? new SecurityException(authResponse.ErrorMessage);
                        throw exception.PreserveStackTrace();
                    }

                    // create a new session
                    var session = _host.SessionManager.CreateServerSession(sessionID, DateTime.Now, authResponse.AuthenticatedIdentity);
                    session.ClientAddress = clientAddress;
                    _host.SessionManager.StoreSession(session);
                    _host.SessionManager.SetCurrentSession(session);

                    _host.OnClientLoggedOn(new LoginEventArgs(LoginEventType.Logon, session.Identity, session.ClientAddress, session.Timestamp));
                }
            }
            catch (Exception ex)
            {
                var args = new LoginEventArgs(LoginEventType.Logon, null, clientAddress, DateTime.Now);
                args.Exception = ex;
                _host.OnClientLogonCanceled(args);
                if (args.Exception != null)
                {
                    // this exception may be translated
                    throw args.Exception.PreserveStackTrace();
                }

                throw;
            }
        }