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


C# ClaimsPrincipal.HasClaim方法代码示例

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


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

示例1: HasClaimCanEditProducts

 public static bool HasClaimCanEditProducts(ClaimsPrincipal user)
 {
     if (user.HasClaim("CanEditProducts", "true"))
     {
         return true;
     };
     return false;
 }
开发者ID:Joshua404,项目名称:eventguru,代码行数:8,代码来源:ClaimsService.cs

示例2: HasClaim_DoesHaveClaim_ReturnsTrue

 public void HasClaim_DoesHaveClaim_ReturnsTrue()
 {
     var ci = new ClaimsIdentity(new Claim[]
     {
         new Claim("type1", "value1"),
         new Claim("type2", "value2"),
     });
     var cp = new ClaimsPrincipal(ci);
     Assert.IsTrue(cp.HasClaim("type1"));
 }
开发者ID:KeithBarrows,项目名称:BrockAllen.MembershipReboot,代码行数:10,代码来源:ClaimsPrincipalExtensionsTests.cs

示例3: PartialWithClaim

 public static IHtmlContent PartialWithClaim(this IHtmlHelper helper, string partialName, object model, ClaimsPrincipal user, ClaimTypeEnum claimType, ClaimValueEnum claimValue)
 {
     if (user.HasClaim(claimType.ToString(), claimValue.ToString()))
     {
         return helper.Partial(partialName, model: model);
     }
     else
     {
         return helper.Raw(string.Empty);
     }
 }
开发者ID:psmyrdek,项目名称:it-learning,代码行数:11,代码来源:HtmlHelperExtensions.cs

示例4: CanSerializeAndDeserializeAClaimsPrincipal

        public void CanSerializeAndDeserializeAClaimsPrincipal()
        {
            var claims = new Claim[]{
                new Claim(Constants.ClaimTypes.Subject, "alice"),
                new Claim(Constants.ClaimTypes.Scope, "read"),
                new Claim(Constants.ClaimTypes.Scope, "write"),
            };
            var ci = new ClaimsIdentity(claims, Constants.AuthenticationMethods.Password);
            var cp = new ClaimsPrincipal(ci);

            var settings = new JsonSerializerSettings();
            settings.Converters.Add(new ClaimsPrincipalConverter());
            var json = JsonConvert.SerializeObject(cp, settings);

            cp = JsonConvert.DeserializeObject<ClaimsPrincipal>(json, settings);
            Assert.Equal(Constants.AuthenticationMethods.Password, cp.Identity.AuthenticationType);
            Assert.Equal(3, cp.Claims.Count());
            Assert.True(cp.HasClaim(Constants.ClaimTypes.Subject, "alice"));
            Assert.True(cp.HasClaim(Constants.ClaimTypes.Scope, "read"));
            Assert.True(cp.HasClaim(Constants.ClaimTypes.Scope, "write"));
        }
开发者ID:giggio,项目名称:TwentyTwenty.IdentityServer4.EntityFramework7,代码行数:21,代码来源:ClaimsPrincipalConverterTests.cs

示例5: NotAuthorizedClaim

 private bool NotAuthorizedClaim(ClaimsPrincipal user)
 {
     return user.IsNotNull() && 
         Claim.IsNotNull() && 
         !user.HasClaim(Claim.Type, Claim.Value);
 }
开发者ID:c4rm4x,项目名称:C4rm4x.WebApi,代码行数:6,代码来源:SecuredAttribute.cs

示例6: UserAccount

        // Constructor, pass in the security principal
        public UserAccount(ClaimsPrincipal user)
        {
            if (HttpContext.Current.Request.IsAuthenticated)
            {
                Principal = user;

                // Extract the role claims
                RoleClaims = user.Claims.Where(c => c.Type == ClaimTypes.Role).Select(c => c.Value);

                // User name
                Name = user.Identity.Name;

                // Extract the given name(s); if null or empty, then set an initial value
                string gn = user.Claims.SingleOrDefault(c => c.Type == ClaimTypes.GivenName).Value;
                if (string.IsNullOrEmpty(gn)) { gn = "(empty given name)"; }
                GivenName = gn;

                // Extract the surname; if null or empty, then set an initial value
                string sn = user.Claims.SingleOrDefault(c => c.Type == ClaimTypes.Surname).Value;
                if (string.IsNullOrEmpty(sn)) { sn = "(empty surname)"; }
                Surname = sn;

                IsAuthenticated = true;
                IsAdmin = user.HasClaim(ClaimTypes.Role, "Admin") ? true : false;
            }
            else
            {
                RoleClaims = new List<string>();
                Name = "anonymous";
                GivenName = "Unauthenticated";
                Surname = "Anonymous";
                IsAuthenticated = false;
                IsAdmin = false;
            }

            NamesFirstLast = $"{GivenName} {Surname}";
            NamesLastFirst = $"{Surname}, {GivenName}";
        }
开发者ID:peteratseneca,项目名称:bti420winter2016,代码行数:39,代码来源:Manager.cs

示例7: GetOutputClaimsIdentity

        /// <summary>
        ///     Produces the output identity that gets transformed into a token
        /// </summary>
        /// <param name="principal">The principal.</param>
        /// <param name="request">The request.</param>
        /// <param name="scope">The scope.</param>
        /// <returns>An IClaimsIdentity describing the subject</returns>
        protected override ClaimsIdentity GetOutputClaimsIdentity(ClaimsPrincipal principal,
            RequestSecurityToken request, Scope scope)
        {
            var requestDetails = (scope as RequestDetailsScope).RequestDetails;

            // externally authenticated user
            if (
                principal.HasClaim(
                    c => c.Type == Constants.Claims.IdentityProvider && c.Issuer == Constants.InternalIssuer))
            {
                Tracing.Information("Issuing a token for an external user.");
                return GetExternalOutputClaims(principal, requestDetails);
            }

            var userClaims = GetOutputClaims(principal, requestDetails, ClaimsRepository);
            var outputIdentity = new ClaimsIdentity(userClaims, "IdSrv");

            if (requestDetails.IsActAsRequest)
            {
                Tracing.Information("Issuing act as token");
                return GetActAsClaimsIdentity(outputIdentity, requestDetails);
            }
            Tracing.Information("Issuing identity token");
            return outputIdentity;
        }
开发者ID:azhuang88,项目名称:IdentityServer,代码行数:32,代码来源:TokenService.cs

示例8: SetPrincipal

        protected virtual void SetPrincipal(HttpRequestMessage request, ClaimsPrincipal principal)
        {
            if (principal.Identity.IsAuthenticated)
            {
                string name = "unknown";

                if (!string.IsNullOrWhiteSpace(principal.Identity.Name))
                {
                    name = principal.Identity.Name;
                }
                else if (principal.HasClaim(c => c.Type == ClaimTypes.NameIdentifier ) && !string.IsNullOrWhiteSpace(principal.FindFirst(ClaimTypes.NameIdentifier).Value))
                {
                    name = principal.Identity.Name;
                }
                else if (principal.HasClaim(c => c.Type == "sub" ) && !string.IsNullOrWhiteSpace(principal.FindFirst("sub").Value))
                {
                    name = principal.Identity.Name;
                }
                else if (principal.Claims.First() != null)
                {
                    name = principal.Claims.First().Value;
                }

                Tracing.Verbose("Principal set for: " + name);
            }
            else
            {
                Tracing.Verbose("Setting anonymous principal.");
            }

            request.GetRequestContext().Principal = principal;

            if (_authN.Configuration.SetPrincipalOnRequestInstance)
            {
                request.Properties[PrincipalKey] = principal;
            }
        }
开发者ID:nhlatour,项目名称:Thinktecture.IdentityModel,代码行数:37,代码来源:AuthenticationHandler.cs


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