コード例 #1
0
        public override IKpiResult Evaluate(object sender, EventArgs e)
        {
            var result = new KpiConversionResult {
                HasConverted = false, KpiId = Id
            };
            HttpContext      current = HttpContext.Current;
            ContentEventArgs val     = e as ContentEventArgs;

            if (current != null && val != null && TestContentGuid == val.Content.ContentGuid)
            {
                if (_visitorGroupRoleRepository.TryGetRole(SelectedVisitorGroup, out var role))
                {
                    result.HasConverted = role.IsMatch(current.User, new HttpContextWrapper(current));
                }
            }

            return(result);
        }
コード例 #2
0
        public override IKpiResult Evaluate(object sender, EventArgs e)
        {
            // Used to evalauate if the user has converted
            // Note: this is called as a result of the event defined in EvaluateProxyEvent
            var result = new KpiConversionResult {
                HasConverted = false, KpiId = Id
            };

            if (HttpContext.Current.User != null)
            {
                VisitorGroupRole role;
                if (_visitorGroupRoleRepository.TryGetRole(SelectedVisitorGroup, out role))
                {
                    // Check if the user has matched the selected visitor group
                    result.HasConverted = role.IsMatch(PrincipalInfo.CurrentPrincipal, new HttpContextWrapper(HttpContext.Current));
                }
            }

            return(result);
        }
コード例 #3
0
        /// <summary>
        /// Adds any Episerver visitor groups roles setup as security groups to claims identity
        /// </summary>
        /// <param name="identity"></param>
        /// <param name="httpContextBase"></param>
        /// <returns></returns>
        public async Task AddVisitorGroupRolesAsClaimsAsync(ClaimsIdentity identity, HttpContextBase httpContextBase = null)
        {
            if (identity == null || string.IsNullOrWhiteSpace(identity.Name))
            {
                throw new ArgumentNullException(nameof(identity));
            }
            var httpContext = httpContextBase ?? new HttpContextWrapper(HttpContext.Current);

            httpContext.User = httpContext.User ?? new ClaimsPrincipal(identity); // its null when user is authenticated...
            var visitorGroups = _visitorGroupRepository.List();

            foreach (var visitorGroup in visitorGroups)
            {
                if (_visitorGroupRoleRepository.TryGetRole(visitorGroup.Name, out var virtualRoleObject) &&
                    virtualRoleObject.IsMatch(httpContext.User, httpContext))
                {
                    identity.AddClaim(new Claim(ClaimTypes.Role, virtualRoleObject.Name));
                }
            }

            await Task.FromResult(0);
        }