当前位置: 首页>>代码示例>>C#>>正文


C# IClaimsPrincipal.HasRequiredClaims方法代码示例

本文整理汇总了C#中IClaimsPrincipal.HasRequiredClaims方法的典型用法代码示例。如果您正苦于以下问题:C# IClaimsPrincipal.HasRequiredClaims方法的具体用法?C# IClaimsPrincipal.HasRequiredClaims怎么用?C# IClaimsPrincipal.HasRequiredClaims使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IClaimsPrincipal的用法示例。


在下文中一共展示了IClaimsPrincipal.HasRequiredClaims方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CheckBusinessUser

        /// <summary>
        /// Check if the user is a business type user and if he has access to the requested business
        /// </summary>
        /// <param name="principal"></param>
        /// <param name="businessId"></param>
        /// <returns>True if user has access</returns>
        private bool CheckBusinessUser(IClaimsPrincipal  principal, long businessId)
        {
            // Calculate if the current user has right on business
            bool isBusinessUser = principal.HasRequiredClaims(new DefaultClaimSet(Claim.CreateNameClaim(ClaimType.RIGHT_ON_BUSINESS)));

            if (isBusinessUser &&
                OperationContext.Current.IncomingMessageProperties != null)
            {
                MembershipUser user = Membership.GetUser(GetUserName());
                List<long> businessIds = new List<long>();
                if (user != null)
                {
                    businessIds = userManager.GetBusinessesForUser((Guid) user.ProviderUserKey);
                }
                // Check the Business User is accessing one of his own businesses
                long primaryBusinessId =
                    Convert.ToInt64(operationContextManager.GetIncomingMessageProperty(PRIMARY_BUSINESS_ID));
                if (primaryBusinessId == businessId ||
                    businessIds.Any(bid => bid == businessId))
                {
                    return true;
                }
            }

            if (isBusinessUser)
            {
                // Business User that is trying to access business other than his primary
                throw new AccessDeniedException(ErrorFactory.CreateAndLogError(Errors.SRVEX20001,
                                                                              "eviivo.Eagle.Services.Core.AbstractService.CheckAccessRights",
                                                                               "No access to business or region.", arguments: new object[] { businessId }));
            }

            // User is not a business user
            return false;
        }
开发者ID:ognjenm,项目名称:egle,代码行数:41,代码来源:AbstractService.cs

示例2: CheckClaims

        /// <summary>
        /// Checks the claims for a given claims principal
        /// </summary>
        /// <param name="principal">Claims Principal</param>
        /// <remarks> Throws a <see cref="T:Eviivo.Business.Exceptions.AccessDeniedException"/> at run time if the security requirement is not met. </remarks>
        public void CheckClaims(IClaimsPrincipal principal)
        {
            //If the IsAuthenticated property is set to true, it checks to see that the current principal 
            //is authenticated.
            System.Security.Principal.IIdentity identity = principal.Identity;
            if (principal != null && isAuthenticated && !identity.IsAuthenticated)
            {
                throw new AccessDeniedException(ErrorFactory.CreateAndLogError(Errors.SRVEX20001, "ClaimsPrincipalPermission.CheckClaims", "Security principal is not authenticated.", arguments: new object[] { principal.Identity.Name, requiredClaims }));
            }
            //If the RequiredClaims property is null, it does not check any further, and the demand is complete, 
            //otherwise, the requirements for issuer and claims are verified.
            if (this.requiredClaims == null)
            {
                return;
            }

            //The Issuer is checked next. When the RequiredClaims property is set, a ClaimSet is provided which 
            //includes a ClaimSet for the Issuer. The assumption here is that the developer has provided one or 
            //more identifying claims about the required Issuer. If one of these claims is matched by the Issuer 
            //of the security principal’s claims, then we have a match.
            //Once an Issuer match is verified the list of required claims are checked. All required claims must
            //be present in the RequiredClaims ClaimSet, or the demand fails.
            if (principal != null && !principal.HasRequiredClaims(this.requiredClaims))
            {
                throw new AccessDeniedException(ErrorFactory.CreateAndLogError(Errors.SRVEX20001, "ClaimsPrincipalPermission.CheckClaims", "Security principal does not satisfy required claims.", arguments: new object[] { identity.Name, principal.Claims.Select(c => c.Resource.ToString()).ToArray().Serialize(), requiredClaims.Select(c => c.Resource.ToString()).ToArray().Serialize() }));
            }
        }
开发者ID:ognjenm,项目名称:egle,代码行数:32,代码来源:ClaimsPrincipalPermission.cs


注:本文中的IClaimsPrincipal.HasRequiredClaims方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。