public UserInfo CreateUserInfo(ClaimsPrincipal claimsPrincipal, string profile)
        {
            Claim extUserIdClaim = claimsPrincipal.FindFirst(SupportedClaimTypes.ExtUserId);
            var email = claimsPrincipal.FindFirst(ClaimTypes.Email).Value;
            UserInfo userInfo = null;
            
            if (extUserIdClaim != null && !string.IsNullOrWhiteSpace(extUserIdClaim.Value))
            {
                var wrapper = new CommonDbWrapper();
                var userId = wrapper.GetUserId(extUserIdClaim.Value, profile);
                if (userId != null)
                {
                    _log.DebugFormat("Updating new user with extUserId: {0}", extUserIdClaim.Value);
                    userInfo = CoreContext.UserManager.GetUsers(Guid.Parse(userId));

                    while (true)
                    {
                        int count = 0;
                        var userByEmail = CoreContext.UserManager.GetUserByEmail(email);
                        if (userByEmail == Constants.LostUser || userByEmail.ID == userInfo.ID)
                        {
                            break;
                        }
                        email += count++;
                    }
                }
                if (userId == null || userInfo == Constants.LostUser)
                {
                    _log.DebugFormat("Creating new user with extUserId: {0}", extUserIdClaim.Value);
                    userInfo = new UserInfo();
                    wrapper.SaveExtUserId(extUserIdClaim.Value, userInfo.ID.ToString(), profile);
                    while (true)
                    {
                        int count = 0;
                        if (CoreContext.UserManager.GetUserByEmail(email) == Constants.LostUser)
                        {
                            break;
                        }
                        email += count++;
                    }
                }
            }
            else
            {
                userInfo = CoreContext.UserManager.GetUserByEmail(email);
                if (userInfo == Constants.LostUser)
                {
                    _log.DebugFormat("Creating new user with email: {0}", email);
                    userInfo = new UserInfo();
                }
                else
                {
                    _log.DebugFormat("Updating user with email: {0}", email);
                }
            }
            Claim givenNameClaim = claimsPrincipal.FindFirst(ClaimTypes.GivenName);
            Claim surNameClaim = claimsPrincipal.FindFirst(ClaimTypes.Surname);
            Claim mobilePhoneClaim = claimsPrincipal.FindFirst(ClaimTypes.MobilePhone);
            Claim titleClaim = claimsPrincipal.FindFirst(SupportedClaimTypes.Title);
            Claim locationClaim = claimsPrincipal.FindFirst(ClaimTypes.StreetAddress);
            Claim birthDateClaim = claimsPrincipal.FindFirst(ClaimTypes.DateOfBirth);
            Claim sexClaim = claimsPrincipal.FindFirst(SupportedClaimTypes.Sex);

            userInfo.ActivationStatus = EmployeeActivationStatus.Activated;
            userInfo.Email = email;
            userInfo.FirstName = givenNameClaim != null ? givenNameClaim.Value : string.Empty;
            userInfo.LastName = surNameClaim != null ? surNameClaim.Value : string.Empty;
            userInfo.MobilePhone = mobilePhoneClaim != null ? mobilePhoneClaim.Value : string.Empty;
            userInfo.Title = titleClaim != null ? titleClaim.Value : string.Empty;
            userInfo.Location = locationClaim != null ? locationClaim.Value : string.Empty;
            string firstName = givenNameClaim != null ? givenNameClaim.Value : string.Empty;
            string lastName = surNameClaim != null ? surNameClaim.Value : string.Empty;
            string mobilePhone = mobilePhoneClaim != null ? mobilePhoneClaim.Value : string.Empty;
            string title = titleClaim != null ? titleClaim.Value : string.Empty;
            string location = locationClaim != null ? locationClaim.Value : string.Empty;
            string birthDateString = birthDateClaim != null ? birthDateClaim.Value : null;
            string sexString = sexClaim != null ? sexClaim.Value : null;

            if (!string.IsNullOrEmpty(firstName))
            {
                if (firstName.Length > MAX_NUMBER_OF_SYMBOLS)
                {
                    firstName = firstName.Substring(0, MAX_NUMBER_OF_SYMBOLS);
                }
                userInfo.FirstName = firstName;
            }
            if (!string.IsNullOrEmpty(lastName))
            {
                if (lastName.Length > MAX_NUMBER_OF_SYMBOLS)
                {
                    lastName = lastName.Substring(0, MAX_NUMBER_OF_SYMBOLS);
                }
                userInfo.LastName = lastName;
            }
            if (!string.IsNullOrEmpty(mobilePhone))
            {
                userInfo.MobilePhone = mobilePhone;
            }
            if (!string.IsNullOrEmpty(title))
            {
                userInfo.Title = title;
            }
            if (!string.IsNullOrEmpty(location))
            {
                userInfo.Location = location;
            }
            if (!string.IsNullOrEmpty(birthDateString))
            {
                try
                {
                    userInfo.BirthDate = DateTime.Parse(birthDateString);
                }
                catch (Exception e)
                {
                    _log.ErrorFormat("Parse birthDateString error: {0}, {1}", e, birthDateString);
                }
            }

            if (!string.IsNullOrEmpty(sexString))
            {
                try
                {
                    userInfo.Sex = Convert.ToBoolean(sexString);
                }
                catch (Exception e)
                {
                    _log.ErrorFormat("Parse sexString error: {0}, {1}", e, sexString);
                }
            }

            if (!userInfo.WorkFromDate.HasValue)
            {
                userInfo.WorkFromDate = TenantUtil.DateTimeNow();
            }

            return userInfo;
        }
        public UserInfo CreateUserInfo(SamlResponse samlResponse)
        {
            var profile = samlResponse.GetIssuer();
            var extUserId = samlResponse.GetExtUserId();
            var email = samlResponse.GetNameID();
            UserInfo userInfo = null;

            if (!string.IsNullOrWhiteSpace(extUserId))
            {
                var wrapper = new CommonDbWrapper();
                var userId = wrapper.GetUserId(extUserId, profile);
                if (userId != null)
                {
                    _log.DebugFormat("Updating new user with extUserId: {0}", extUserId);
                    userInfo = CoreContext.UserManager.GetUsers(Guid.Parse(userId));

                    while (true)
                    {
                        int count = 0;
                        var userByEmail = CoreContext.UserManager.GetUserByEmail(email);
                        if (userByEmail == Constants.LostUser || userByEmail.ID == userInfo.ID)
                        {
                            break;
                        }
                        email += count++;
                    }
                }
                if (userId == null || userInfo == Constants.LostUser)
                {
                    _log.DebugFormat("Creating new user with extUserId: {0}", extUserId);
                    userInfo = new UserInfo { ID = Guid.NewGuid() };
                    wrapper.SaveExtUserId(userInfo.ID.ToString(), extUserId, profile);
                    while (true)
                    {
                        int count = 0;
                        if (CoreContext.UserManager.GetUserByEmail(email) == Constants.LostUser)
                        {
                            break;
                        }
                        email += count++;
                    }
                }
            }
            else
            {
                userInfo = CoreContext.UserManager.GetUserByEmail(email);
                if (userInfo == Constants.LostUser)
                {
                    _log.DebugFormat("Creating new user with email: {0}", email);
                    userInfo = new UserInfo();
                }
                else
                {
                    _log.DebugFormat("Updating user with email: {0}", email);
                }
            }
            userInfo.Email = email;
            userInfo.ActivationStatus = EmployeeActivationStatus.Activated;

            string firstName = samlResponse.GetFirstName();
            string lastName = samlResponse.GetLastName();
            string mobilePhone = samlResponse.GetMobilePhone();
            string title = samlResponse.GetTitle();
            string location = samlResponse.GetStreetAddress();
            string birthDateString = samlResponse.GetBirthDate();
            string sexString = samlResponse.GetSex();

            if (!string.IsNullOrEmpty(firstName))
            {
                if (firstName.Length > MAX_NUMBER_OF_SYMBOLS)
                {
                    firstName = firstName.Substring(0, MAX_NUMBER_OF_SYMBOLS);
                }
                userInfo.FirstName = firstName;
            }
            if (!string.IsNullOrEmpty(lastName))
            {
                if (lastName.Length > MAX_NUMBER_OF_SYMBOLS)
                {
                    lastName = lastName.Substring(0, MAX_NUMBER_OF_SYMBOLS);
                }
                userInfo.LastName = lastName;
            }
            if (!string.IsNullOrEmpty(mobilePhone))
            {
                userInfo.MobilePhone = mobilePhone;
            }
            if (!string.IsNullOrEmpty(title))
            {
                userInfo.Title = title;
            }
            if (!string.IsNullOrEmpty(location))
            {
                userInfo.Location = location;
            }
            if (!string.IsNullOrEmpty(birthDateString))
            {
                try
                {
                    userInfo.BirthDate = DateTime.Parse(birthDateString);
                }
                catch (Exception e)
                {
                    _log.ErrorFormat("Parse birthDateString error: {0}, {1}", e, birthDateString);
                }
            }

            if (!string.IsNullOrEmpty(sexString))
            {
                try
                {
                    userInfo.Sex = Convert.ToBoolean(sexString);
                }
                catch (Exception e)
                {
                    _log.ErrorFormat("Parse sexString error: {0}, {1}", e, sexString);
                }
            }

            if (!userInfo.WorkFromDate.HasValue)
            {
                userInfo.WorkFromDate = TenantUtil.DateTimeNow();
            }

            return userInfo;
        }