public IUser GetAuthenticatedUser()
        {
            if (_signedInUser != null || _isAuthenticated)
            {
                return(_signedInUser);
            }

            var httpContext = _httpContextAccessor.Current();

            if (httpContext == null || !httpContext.Request.IsAuthenticated)
            {
                return(null);
            }

            if (httpContext.User.Identity is FormsIdentity)
            {
                var formsIdentity = (FormsIdentity)httpContext.User.Identity;
                var userData      = formsIdentity.Ticket.UserData ?? "";

                // the cookie user data is {userId};{tenant}
                var userDataSegments = userData.Split(';');

                if (userDataSegments.Length != 2)
                {
                    return(null);
                }

                var userDataId     = userDataSegments[0];
                var userDataTenant = userDataSegments[1];

                if (!String.Equals(userDataTenant, _settings.Name, StringComparison.Ordinal))
                {
                    return(null);
                }

                int userId;
                if (!int.TryParse(userDataId, out userId))
                {
                    Logger.Fatal("User id not a parsable integer");
                    return(null);
                }

                _isAuthenticated = true;
                return(_signedInUser = _contentManager.Get(userId).As <IUser>());
            }
            else if (httpContext.User is CasPrincipal)
            {
                using (var context = new OrchardEntities())
                {
                    var casPrincipal = (CasPrincipal)httpContext.User;

                    var UserAccount = context.Orchard_Users_UserPartRecord.FirstOrDefault(x => x.Email.ToLower() == casPrincipal.MaxAttributes.EmailAddress.ToLower());

                    if (UserAccount == null)
                    {
                        UserAccount = context.Orchard_Users_UserPartRecord.FirstOrDefault(x => x.UserName == "DefaultAccountDoNotDelete");
                    }

                    var orchardUser = _contentManager.Query("User").List().Select(x => x.As <IUser>()).First(x => x.Email.ToLower() == UserAccount.Email.ToLower());

                    return(orchardUser);
                }
            }
            return(null);
        }
        public IUser GetAuthenticatedUser() {
            if (_signedInUser != null || _isAuthenticated)
                return _signedInUser;

            var httpContext = _httpContextAccessor.Current();
            if (httpContext == null || !httpContext.Request.IsAuthenticated)
                return null;

            if (httpContext.User.Identity is FormsIdentity) {
                var formsIdentity = (FormsIdentity)httpContext.User.Identity;
                var userData = formsIdentity.Ticket.UserData ?? "";

                // the cookie user data is {userId};{tenant}
                var userDataSegments = userData.Split(';');

                if (userDataSegments.Length != 2) {
                    return null;
                }

                var userDataId = userDataSegments[0];
                var userDataTenant = userDataSegments[1];

                if (!String.Equals(userDataTenant, _settings.Name, StringComparison.Ordinal)) {
                    return null;
                }

                int userId;
                if (!int.TryParse(userDataId, out userId)) {
                    Logger.Fatal("User id not a parsable integer");
                    return null;
                }

                _isAuthenticated = true;
                return _signedInUser = _contentManager.Get(userId).As<IUser>();
            }
            else if (httpContext.User is CasPrincipal) {

                using (var context = new OrchardEntities())
                {

                    var casPrincipal = (CasPrincipal)httpContext.User;

                    var UserAccount = context.Orchard_Users_UserPartRecord.FirstOrDefault(x => x.Email.ToLower() == casPrincipal.MaxAttributes.EmailAddress.ToLower());
                    
                    if (UserAccount == null)
                        UserAccount = context.Orchard_Users_UserPartRecord.FirstOrDefault(x => x.UserName == "DefaultAccountDoNotDelete");

                    var orchardUser = _contentManager.Query("User").List().Select(x => x.As<IUser>()).First(x => x.Email.ToLower() == UserAccount.Email.ToLower());
                    
                    return orchardUser;

                }
                
            }
            return null;
        }