public ConfiguredSessionObject(SessionObject session, SessionConfigGroup userCfg, LoginMode mode)
 {
     ApplicationID = session.ApplicationID;
     SessionID = session.SessionID;
     CRMID = session.CRMID;
     SecurityEntityID = session.SecurityEntityID;
     CompanyID = session.CompanyID;
     Username = session.Username;
     Password = session.Password;
     Impersonation = session.Impersonation;
     PermissionsList = session.PermissionsList;
     Locale = userCfg.Locale;
     EntityName = session.EntityName;
     EntityType = session.EntityType;
     TimeZoneKey = session.TimeZoneKey ?? "UTC";
     EnableTimeZoneSelect = session.EnableTimeZoneSelect;
     _configuration = userCfg;
     Initialize(mode);
 }
        public GetItemResponse<ConfiguredSessionObject> Login(SessionRequest request)
        {
            try
            {
                SessionObject session;
                if (request.Username != null)
                {
                    if (request.SessionID != Guid.Empty)
                    {
                        Logout(request);
                        request.SessionID = Guid.Empty;
                    }
                    // this clears old session id if user name is used to log in otherwise we are logging in with somebody else's old session id!!
                    session = null;
                }
                else
                {
                    session = SessionObjectCache.Instance.GetSession(request.SessionID);
                }

                if (session == null)
                {
                    GetItemResponse<SessionObject> resp1 = null;
                    var service0 = ImardaProxyManager.Instance.IImardaSecurityProxy;
                    ChannelInvoker.Invoke(delegate(out IClientChannel channel)
                    {
                        channel = service0 as IClientChannel;
                        resp1 = service0.GetSessionObject(request);
                        if (request.Mode == LoginMode.Normal) ErrorHandler.Check(resp1);
                        session = resp1.Item;
                    });
                    if (session == null)
                    {
                        var statusMessage = "Authentication failed";
                        if (resp1 != null)
                        {
                            if (resp1.ErrorCode == "-1")
                                statusMessage = "Invalid user";
                            if (resp1.ErrorCode == "0")
                                statusMessage = "Success";
                            else if (resp1.ErrorCode == "1")
                                statusMessage = "Invalid username";
                            else if (resp1.ErrorCode == "2")
                                statusMessage = "User has no login permission";
                            if (resp1.ErrorCode == "3")
                                statusMessage = "Login is disabled";
                            else if (resp1.ErrorCode == "4")
                                statusMessage = "Invalid password";
                        }
                        return new GetItemResponse<ConfiguredSessionObject> { Status = false, StatusMessage = statusMessage, ErrorCode = resp1.ErrorCode };
                    }
                }
                else
                {
                    var service1 = ImardaProxyManager.Instance.IImardaSecurityProxy;
                    ChannelInvoker.Invoke(delegate(out IClientChannel channel3)
                    {
                        channel3 = service1 as IClientChannel;
                        var resp3 = service1.GetSecurityEntity(new IDRequest(session.SecurityEntityID));
                        SecurityEntity se = resp3.Item;
                        session.TimeZoneKey = se.TimeZone;
                    });
                }

                var config = new SessionConfigGroup();

                var service2 = ImardaProxyManager.Instance.IImardaConfigurationProxy;
                ConfiguredSessionObject cfgSession = null;
                ChannelInvoker.Invoke(delegate(out IClientChannel channel2)
                {
                    channel2 = service2 as IClientChannel;
                    Guid[] ids = ConfigGroup.GetIDs(config);
                    var request2 = new ConfigListRequest(ids, session.CompanyID, session.CRMID);
                    service2.RemoveFromCache(request2);
                    var resp2b = service2.GetConfigValueList(request2);
                    if (request.Mode == LoginMode.Normal) ErrorHandler.Check(resp2b);
                    ConfigValue[] values = resp2b.List.ToArray();
                    ConfigGroup.SetValues(config, values);

                    cfgSession = new ConfiguredSessionObject(session, config, request.Mode);
                    config.PreferredMeasurementUnits = CultureHelper.CalcPreferences(config, service2);

                    var cacheSession = cfgSession.StripConfig(request.Mode);
                    SessionObjectCache.Instance.StoreSession(cacheSession);
                });

                var sb = new StringBuilder();
                sb.AppendKV("User", cfgSession.Username)
                    .AppendKV("AppID", cfgSession.ApplicationID.ToString().ToUpperInvariant())
                    .AppendKV("Locale", cfgSession.PreferredCulture) // used in formatter for notification templates!
                    .AppendKV("Region", cfgSession.Configuration.Region)
                    .AppendKV("Impers", cfgSession.Impersonation)
                    .AppendKV("EvTime", DateTime.UtcNow, "~");
                var logonEventID = new Guid("4c2f21cb-fcdd-4d6b-a6bd-a928680bee05");

                AlertTaskHelper.SaveAlertTask(
                    cfgSession.CompanyID,
                    logonEventID,
                    cfgSession.CRMID,
                    cfgSession.CRMID,
                    cfgSession.Username,
                    Guid.Empty,
                    sb,
                    TimeZoneInfo.FindSystemTimeZoneById(cfgSession.TimeZoneKey),
                    Guid.Empty);

                cfgSession.Password = null; // clear password hash before returning
                return new GetItemResponse<ConfiguredSessionObject>
                {
                    Item = cfgSession,
                    Status = true
                };
            }
            catch (Exception ex)
            {
                return ErrorHandler.Handle<GetItemResponse<ConfiguredSessionObject>>(ex);
            }
        }