/// <summary> /// Determines whether [is user roles valid] [the specified user]. /// </summary> /// <param name="user">The user.</param> /// <param name="allowedRoles">The allowed roles.</param> /// <returns> /// <c>true</c> if [is user roles valid] [the specified user]; otherwise, <c>false</c>. /// </returns> public static Boolean IsUserRolesValid(ClaimsPrincipal user, String[] allowedRoles) { if (ClaimsHelper.IsPasswordToken(user) == false) { return(true); } return(allowedRoles.Any(r => user.IsInRole(r))); }
/// <summary> /// Gets the user claims. /// </summary> /// <param name="user">The user.</param> /// <param name="customClaimType">Type of the custom claim.</param> /// <param name="defaultValue">The default value.</param> /// <returns></returns> /// <exception cref="InvalidOperationException">No claim [{customClaimType}] found for user id [{userIdClaim.Value}</exception> public static Claim GetUserClaim(ClaimsPrincipal user, String customClaimType, String defaultValue = "") { Claim userClaim = null; if (ClaimsHelper.IsPasswordToken(user)) { // Get the claim from the token userClaim = user.Claims.SingleOrDefault(c => c.Type.ToLower() == customClaimType.ToLower()); if (userClaim == null) { throw new NotFoundException($"Claim type [{customClaimType}] not found"); } } else { userClaim = new Claim(customClaimType, defaultValue); } return(userClaim); }