本文整理汇总了C#中System.Security.Claims.ClaimsIdentity.HasClaim方法的典型用法代码示例。如果您正苦于以下问题:C# ClaimsIdentity.HasClaim方法的具体用法?C# ClaimsIdentity.HasClaim怎么用?C# ClaimsIdentity.HasClaim使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Claims.ClaimsIdentity
的用法示例。
在下文中一共展示了ClaimsIdentity.HasClaim方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateRolesForClaims
public static IEnumerable<Claim> CreateRolesForClaims(ClaimsIdentity user) {
List<Claim> claims = new List<Claim>();
if (user.HasClaim(x => x.Type == ClaimTypes.StateOrProvince && x.Issuer == "RemoteClaims" && x.Value == "DC")
&& user.HasClaim(x => x.Type == ClaimTypes.Role && x.Value == "Employees")) {
claims.Add(new Claim(ClaimTypes.Role, "DCStaff"));
}
return claims;
}
示例2: CreateRolesBasedOnClaims
public static IEnumerable<Claim> CreateRolesBasedOnClaims(ClaimsIdentity identity)
{
List<Claim> claims = new List<Claim>();
if(identity.HasClaim(c => c.Type == "FTE" && c.Value == "1") && identity.HasClaim(ClaimTypes.Role, "Admin"))
{
claims.Add(new Claim(ClaimTypes.Role, "IncidentResolvers"));
}
return claims;
}
示例3: GetClaims
public async static Task<IEnumerable<Claim>> GetClaims(ClaimsIdentity user)
{
List<Claim> claims = new List<Claim>();
//Only client account will be processed for this type of claim
if (user != null && user.IsAuthenticated && user.HasClaim(c => c.Type == ClaimTypes.Role && c.Value == AuthorizationRoles.Role_Client))
{
using (edisDbEntities db = new edisDbEntities())
{
var userManager = new ApplicationUserManager(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var userProfile = await userManager.FindByNameAsync(user.Name);
if (userProfile != null)
{
var profile = db.RiskProfiles.FirstOrDefault(c => c.Client.ClientUserID == userProfile.Id);
if (profile != null)
{
claims.Add(CreateClaim(ClaimType_clientRiskProfileCompletion, ClaimValue_clientRiskProfile_Completed));
}
else
{
claims.Add(CreateClaim(ClaimType_clientRiskProfileCompletion, ClaimValue_clientRiskProfile_Incomplete));
}
}
}
}
return claims;
}
示例4: CreateIdentityAsync
public Task<ClaimsIdentity> CreateIdentityAsync(User user, string authenticationType, ClaimsIdentity externalIdentity)
{
var identity = new ClaimsIdentity(authenticationType, ClaimTypes.Name, ClaimTypes.Role);
identity.AddClaims(externalIdentity.Claims);
identity.AddClaim(new Claim(ClaimTypes.Name, user.Name, ClaimValueTypes.String));
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id.ToString(), ClaimValueTypes.String));
if (user.IsAdmin)
{
identity.AddClaim(new Claim(ClaimTypes.Role, "admin", ClaimValueTypes.String));
}
if (externalIdentity.HasClaim(claim => claim.Type == "urn:reddit:moderator_of"))
{
identity.AddClaim(new Claim(ClaimTypes.Role, "moderator", ClaimValueTypes.String));
}
if (user.IsBanned)
{
identity.AddClaim(new Claim(ClaimTypes.Role, "banned", ClaimValueTypes.String));
}
return Task.FromResult(identity);
}
示例5: GetClaims
public async static Task<IEnumerable<Claim>> GetClaims(ClaimsIdentity user)
{
List<Claim> claims = new List<Claim>();
using (edisDbEntities db = new edisDbEntities())
{
if (user.HasClaim(c => c.Type == ClaimTypes.Role && c.Value == AuthorizationRoles.Role_Client))
{
var userManager = new ApplicationUserManager(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var userProfile = await userManager.FindByNameAsync(user.Name);
var client = db.Clients.FirstOrDefault(c => c.ClientUserID == userProfile.Id);
if (client != null)
{
var clientGroup = db.ClientGroups.FirstOrDefault(c => c.ClientGroupID == client.ClientGroupID);
if (clientGroup != null && clientGroup.MainClientID == client.ClientUserID)
{
claims.Add(CreateClaim(ClaimType_ClientGroupLeader, ClaimValue_ClientGroupLeader_Leader));
}
else
{
claims.Add(CreateClaim(ClaimType_ClientGroupLeader, ClaimValue_ClientGroupLeader_Member));
}
}
}
}
return claims;
}
示例6: GetModules
private List<string> GetModules(ClaimsIdentity ci)
{
if (ci.HasClaim(c => c.Type == IlluminateClaimTypes.OrgModules))
{
var concatModules = ci.FindFirst(c => c.Type == IlluminateClaimTypes.OrgModules).Value;
return Organisation.DecatenateModules(concatModules);
}
return new List<string> { "sickness", "leave" };
}
示例7: CheckIdentityAndBasicClaims
private bool CheckIdentityAndBasicClaims(ClaimsIdentity ci)
{
if (ci == null)
return false;
if (!ci.HasClaim(c => c.Type == ClaimTypes.NameIdentifier))
{
GetLogger().Info("NameIdentifier claim not present");
return false;
}
return true;
}
示例8: SetExternalProperties
private async Task SetExternalProperties(ClaimsIdentity identity, ClaimsIdentity ext)
{
if (ext != null)
{
var ignoreClaim = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims";
// Adicionando Claims Externos no Identity
foreach (var c in ext.Claims)
{
if (!c.Type.StartsWith(ignoreClaim))
if (!identity.HasClaim(c.Type, c.Value))
identity.AddClaim(c);
}
}
}
示例9: GrantResourceOwnerCredentials
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
var user = await UserManager.FindAsync(context.UserName, context.Password);
var data = await context.Request.ReadFormAsync();
if (user == null)
{
context.SetError("invalid_grant", "The user name or password is incorrect.");
return;
}
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
identity.AddClaim(new Claim("userId", user.Id.ToString()));
foreach (var role in UserManager.GetRoles(user.Id))
{
identity.AddClaim(new Claim(ClaimTypes.Role, role.ToString()));
}
foreach (var claim in UserManager.GetClaims(user.Id))
{
identity.AddClaim(new Claim(claim.Type, claim.Value));
}
var isSuperAdmin = identity.HasClaim(ClaimTypes.Role, RoleClassification.SuperAdministrator.ToString());
Guid cloakCompanyId;
Guid companyId;
if (data["cloakCompanyId"] != null && Guid.TryParse(data["cloakCompanyId"], out cloakCompanyId) && isSuperAdmin)
{
// Super administrator cloaking of a company.
companyId = cloakCompanyId;
}
else
{
// Typical user sign in.
companyId = user.CompanyId;
}
identity.AddClaim(new Claim("companyId", companyId.ToString()));
identity.AddClaim(new Claim("isSuper", isSuperAdmin.ToString()));
var properties = CreateProperties(user.Id, companyId, isSuperAdmin);
var ticket = new AuthenticationTicket(identity, properties);
context.Validated(ticket);
}
示例10: AuthorizeAdministration
protected virtual bool AuthorizeAdministration(Collection<Claim> resource, ClaimsIdentity id)
{
var roleResult = id.HasClaim(ClaimTypes.Role, Constants.Roles.IdentityServerAdministrators);
if (!roleResult)
{
if (resource[0].Value != Constants.Resources.UI)
{
Tracing.Error(
string.Format("Administration authorization failed because user {0} is not in the {1} role",
id.Name, Constants.Roles.IdentityServerAdministrators));
}
}
return roleResult;
}
示例11: AuthorizeTokenIssuance
protected virtual bool AuthorizeTokenIssuance(Collection<Claim> resource, ClaimsIdentity id)
{
if (!id.IsAuthenticated)
{
logger.Error("Authorization for token issuance failed because the user is anonymous");
return false;
}
if (ConfigurationRepository.Global.EnforceUsersGroupMembership)
{
var roleResult = id.HasClaim(ClaimTypes.Role, Constants.Roles.IdentityServerUsers);
if (!roleResult)
{
logger.Error(string.Format("Authorization for token issuance failed because user {0} is not in the {1} role", id.Name, Constants.Roles.IdentityServerUsers));
}
return roleResult;
}
return true;
}
示例12: GrantResourceOwnerCredentials
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
string user = context.UserName;
string password = context.Password;
Usuario dbUser = this.db.Usuario.Where(u => u.login == context.UserName).FirstOrDefault();
if (password != "password")
{
context.SetError("invalid_grant", "Contrseña incorrecta.");
return;
}
if (dbUser == null)
{
context.SetError("invalid_grant", "Usuario inexistente.");
return;
}
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
foreach (RolxUsuario ur in dbUser.RolxUsuario )
{
foreach (FuncionXRol rp in ur.Rol.FuncionXRol)
{
if (!identity.HasClaim(ClaimTypes.Role, rp.Funcion.Descripcion))
{
identity.AddClaim(new Claim(ClaimTypes.Role, rp.Funcion.Descripcion));
}
}
}
context.Validated(identity);
}
示例13: GetIdentityIsAuthenticated
public bool GetIdentityIsAuthenticated(ClaimsIdentity identity)
{
var result = identity.HasClaim(c => c.Type == IsKnownClaimType && c.Issuer == ClaimIssuer && c.Value.Equals("true", StringComparison.OrdinalIgnoreCase));
return result;
}
示例14: StoreExternalClaims
private async Task<ClaimsIdentity> StoreExternalClaims(ClaimsIdentity userIdentity) {
ClaimsIdentity externalIdentity = await AuthenticationManager.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);
if (externalIdentity != null) {
var currentClaims = await UserManager.GetClaimsAsync(userIdentity.GetUserId());
foreach (var claim in externalIdentity.Claims) {
if (!claim.Type.StartsWith("http://schemas.xmlsoap.org/ws/2005/05/identity/claims")) {
if (userIdentity.HasClaim(c => c.Type == claim.Type && c.Issuer.Contains("LOCAL"))) {
var toRemoveClaim = userIdentity.Claims.FirstOrDefault(c => c.Type == claim.Type && c.Issuer.Contains("LOCAL"));
if (toRemoveClaim != null) {
userIdentity.RemoveClaim(toRemoveClaim);
}
}
if (!userIdentity.HasClaim(claim.Type, claim.Value)) {
// Add to claims
userIdentity.AddClaim(claim);
// Remove current claim
var currentClaim = currentClaims.FirstOrDefault(c => c.Type == claim.Type);
if (currentClaim != null)
await UserManager.RemoveClaimAsync(userIdentity.GetUserId(), currentClaim);
// Store claim to database
await UserManager.AddClaimAsync(userIdentity.GetUserId(), claim);
}
}
}
}
return userIdentity;
}
示例15: SetExternalProperties
private async Task SetExternalProperties(ClaimsIdentity identity)
{
// get external claims captured in Startup.ConfigureAuth
ClaimsIdentity ext = await AuthenticationManager.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);
if (ext != null)
{
var ignoreClaim = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims";
// add external claims to identity
foreach (var c in ext.Claims)
{
if (!c.Type.StartsWith(ignoreClaim))
if (!identity.HasClaim(c.Type, c.Value))
identity.AddClaim(c);
}
}
}