本文整理汇总了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;
}
示例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"));
}
示例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);
}
}
示例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);
}
示例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}";
}
示例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;
}
示例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;
}
}