Esempio n. 1
0
 public UserSession(UserSession speciman)
 {
     login = speciman.login;
     ip = speciman.ip;
     terminalId = speciman.terminalId;
     loginTime = speciman.loginTime;
     sessionTag = speciman.sessionTag;
     lastRequestClientTime = speciman.lastRequestClientTime;
     callback = speciman.callback;
     accountId = speciman.accountId;
     enabledAccounts = speciman.enabledAccounts.ToList();
     terminalVersion = speciman.terminalVersion;
     userId = speciman.userId;
 }
        /// <summary>
        /// вернуть тег сессии клиента, передаваемый затем в последующих запросах
        /// </summary>
        public AuthenticationResponse Authenticate(string login, string hashString, 
            string terminalVersion,
            long clientLocalTime, long terminalId, string address, ITradeSharpServerCallback callback,
            out int sessionTag)
        {
            sessionTag = 0;

            // проверка параметров
            if (string.IsNullOrEmpty(login)) return AuthenticationResponse.InvalidAccount;
            if (string.IsNullOrEmpty(hashString)) return AuthenticationResponse.WrongPassword;
            if (terminalId == 0)
            {
                Logger.Info("AuthenticationResponse usr (" + login + "): terminal Id is 0");
                return AuthenticationResponse.InvalidAccount;
            }
            if (clientLocalTime == 0)
            {
                Logger.Info("AuthenticationResponse usr (" + login + "): clientLocalTime is 0");
                return AuthenticationResponse.InvalidAccount;
            }

            // получить пользователя и его роль по логину
            string password;
            int userId;
            var response = CheckCredentials(login, out password, out userId);
            if (response != AuthenticationResponse.OK) return response;

            // проверить хеш из логина, пароля и локального времени клиента
            var userHash = CredentialsHash.MakeCredentialsHash(login, password, clientLocalTime);
            if (hashString != userHash) return AuthenticationResponse.WrongPassword;

            // проверить наличие сессии
            try
            {
                sessionLocker.AcquireWriterLock(SessionLockTimeout);
            }
            catch (ApplicationException)
            {
                Logger.Error("Authenticate - unable to get writer lock to session storage");
                return AuthenticationResponse.ServerError;
            }
            try
            {
                UserSession session;
                sessions.TryGetValue(terminalId, out session);
                // сессия еще жива
                if (session != null)
                {
                    session.lastRequestClientTime = clientLocalTime;
                    session.callback = callback;
                    sessionTag = session.sessionTag;
                    session.enabledAccounts = GetUserAccounts(userId);
                    return AuthenticationResponse.OK;
                }
                // создать новую сессию
                session = new UserSession
                              {
                                  ip = address,
                                  lastRequestClientTime = clientLocalTime,
                                  login = login,
                                  loginTime = DateTime.Now,
                                  sessionTag = (int) (DateTime.Now.Ticks/3),
                                  terminalId = terminalId,
                                  callback = callback,
                                  enabledAccounts = GetUserAccounts(userId),
                                  terminalVersion = terminalVersion,
                                  userId = userId
                              };
                sessions.Add(terminalId, session);
                sessionTag = session.sessionTag;
                return AuthenticationResponse.OK;
            }
            catch (Exception ex)
            {
                Logger.Error("UserSessionStorage - error in Authenticate", ex);
                return AuthenticationResponse.ServerError;
            }
            finally
            {
                sessionLocker.ReleaseWriterLock();
            }
        }
        public void ReviveChannel(ProtectedOperationContext ctx, ITradeSharpServerCallback callback, 
            string address, string login, int accountId, string terminalVersion)
        {
            int userId;
            string password;
            var response = CheckCredentials(login, out password, out userId);
            if (response != AuthenticationResponse.OK)
            {
                return;
            }

            try
            {
                sessionLocker.AcquireWriterLock(SessionLockTimeout);
            }
            catch (ApplicationException)
            {
                Logger.Error("ReviveChannel - unable to get writer lock to session storage");
                return;
            }
            try
            {
                UserSession session;
                sessions.TryGetValue(ctx.terminalId, out session);
                // сессия еще жива
                if (session != null)
                {
                    session.callback = callback;
                    session.accountId = accountId;
                    return;
                }
                // создать новую сессию
                session = new UserSession
                {
                    ip = address,
                    lastRequestClientTime = ctx.clientLocalTime,
                    login = login,
                    accountId = accountId,
                    loginTime = DateTime.Now,
                    sessionTag = (int)(DateTime.Now.Ticks / 3),
                    terminalId = ctx.terminalId,
                    callback = callback,
                    enabledAccounts = GetUserAccounts(userId),
                    terminalVersion = terminalVersion
                };
                sessions.Add(ctx.terminalId, session);
            }
            catch (Exception ex)
            {
                Logger.Error("ReviveChannel - error", ex);
            }
            finally
            {
                sessionLocker.ReleaseWriterLock();
            }
        }