public void RunClaimActions(ClaimActionCollection claimActions, ClaimsIdentity claimsIdentity, JsonElement userData) { foreach (var action in claimActions) { action.Run(userData, claimsIdentity, Options.ClaimsIssuer ?? Scheme.Name); } }
/// <summary> /// 将微信返回的JSON属性映射到<see cref="ClaimsPrincipal"/> /// 默认值: /// ClaimTypes.Name : openid /// ClaimTypes.NameIdentifier : openid。 /// 这里假设我们已绑定开发者账号,微信返回的JSON含有unionid属性。 /// </summary> /// <param name="claimActions"></param> private void MapWeChatJsonKey(ClaimActionCollection claimActions) { claimActions.Clear(); claimActions.MapJsonKey(ClaimTypes.Name, "unionid"); claimActions.MapJsonKey(ClaimTypes.NameIdentifier, "unionid"); claimActions.MapJsonKey(WeChatClaimTypes.UnionId, "unionid"); claimActions.MapJsonKey(WeChatClaimTypes.OpenId, "openid"); }
public void MapScopes_AddsClaimAction() { ClaimActionCollection col = new ClaimActionCollection(); col.MapScopes(); Assert.Single(col); Assert.IsType <CloudFoundryScopeClaimAction>(col.FirstOrDefault()); }
/// <summary> /// Select a top level value from the json user data with the given key name and add it as a Claim. /// This no-ops if the key is not found or the value is empty. /// </summary> /// <param name="collection">The <see cref="ClaimActionCollection"/>.</param> /// <param name="claimType">The value to use for Claim.Type when creating a Claim.</param> /// <param name="jsonKey">The top level key to look for in the json user data.</param> public static void MapJsonKey(this ClaimActionCollection collection, string claimType, string jsonKey) { if (collection == null) { throw new ArgumentNullException(nameof(collection)); } collection.MapJsonKey(claimType, jsonKey, ClaimValueTypes.String); }
/// <summary> /// Select a second level value from the json user data with the given top level key name and second level sub key name and add it as a Claim. /// This no-ops if the keys are not found or the value is empty. /// </summary> /// <param name="collection">The <see cref="ClaimActionCollection"/>.</param> /// <param name="claimType">The value to use for Claim.Type when creating a Claim.</param> /// <param name="jsonKey">The top level key to look for in the json user data.</param> /// <param name="subKey">The second level key to look for in the json user data.</param> /// <param name="valueType">The value to use for Claim.ValueType when creating a Claim.</param> public static void MapJsonSubKey(this ClaimActionCollection collection, string claimType, string jsonKey, string subKey, string valueType) { if (collection == null) { throw new ArgumentNullException(nameof(collection)); } collection.Add(new JsonSubKeyClaimAction(claimType, valueType, jsonKey, subKey)); }
/// <summary> /// Run the given resolver to select a value from the json user data to add as a claim. /// This no-ops if the returned value is empty. /// </summary> /// <param name="collection">The <see cref="ClaimActionCollection"/>.</param> /// <param name="claimType">The value to use for Claim.Type when creating a Claim.</param> /// <param name="resolver">The Func that will be called to select value from the given json user data.</param> public static void MapCustomJson(this ClaimActionCollection collection, string claimType, Func <JsonElement, string?> resolver) { if (collection == null) { throw new ArgumentNullException(nameof(collection)); } collection.MapCustomJson(claimType, ClaimValueTypes.String, resolver); }
public static void MapScopes(this ClaimActionCollection collection, string claimType = "scope") { if (collection == null) { throw new ArgumentNullException(nameof(collection)); } collection.Add(new CloudFoundryScopeClaimAction(claimType, ClaimValueTypes.String)); }
/// <summary> /// Delete all claims from the given ClaimsIdentity with the given ClaimType. /// </summary> /// <param name="collection">The <see cref="ClaimActionCollection"/>.</param> /// <param name="claimType">The claim type to delete</param> public static void DeleteClaim(this ClaimActionCollection collection, string claimType) { if (collection == null) { throw new ArgumentNullException(nameof(collection)); } collection.Add(new DeleteClaimAction(claimType)); }
/// <summary> /// Clears any current ClaimsActions and maps all values from the json user data as claims, excluding duplicates. /// </summary> /// <param name="collection">The <see cref="ClaimActionCollection"/>.</param> public static void MapAll(this ClaimActionCollection collection) { if (collection == null) { throw new ArgumentNullException(nameof(collection)); } collection.Clear(); collection.Add(new MapAllClaimsAction()); }
/// <summary> /// Clears any current ClaimsActions and maps all values from the json user data as claims, excluding the specified types. /// </summary> /// <param name="collection">The <see cref="ClaimActionCollection"/>.</param> /// <param name="exclusions">The types to exclude.</param> public static void MapAllExcept(this ClaimActionCollection collection, params string[] exclusions) { if (collection == null) { throw new ArgumentNullException(nameof(collection)); } collection.MapAll(); collection.DeleteClaims(exclusions); }
/// <summary> /// Delete all claims from the ClaimsIdentity with the given claimTypes. /// </summary> /// <param name="collection"></param> /// <param name="claimTypes"></param> public static void DeleteClaims(this ClaimActionCollection collection, params string[] claimTypes) { if (claimTypes == null) { throw new ArgumentNullException(nameof(claimTypes)); } foreach (var claimType in claimTypes) { collection.Add(new DeleteClaimAction(claimType)); } }
public static void MapAbpClaimTypes(this ClaimActionCollection claimActions) { if (AbpClaimTypes.UserName != "name") { claimActions.MapJsonKey(AbpClaimTypes.UserName, "name"); claimActions.DeleteClaim("name"); claimActions.RemoveDuplicate(AbpClaimTypes.UserName); } if (AbpClaimTypes.Name != "given_name") { claimActions.MapJsonKey(AbpClaimTypes.Name, "given_name"); claimActions.DeleteClaim("given_name"); claimActions.RemoveDuplicate(AbpClaimTypes.Name); } if (AbpClaimTypes.SurName != "family_name") { claimActions.MapJsonKey(AbpClaimTypes.SurName, "family_name"); claimActions.DeleteClaim("family_name"); claimActions.RemoveDuplicate(AbpClaimTypes.SurName); } if (AbpClaimTypes.Email != "email") { claimActions.MapJsonKey(AbpClaimTypes.Email, "email"); claimActions.DeleteClaim("email"); claimActions.RemoveDuplicate(AbpClaimTypes.Email); } if (AbpClaimTypes.EmailVerified != "email_verified") { claimActions.MapJsonKey(AbpClaimTypes.EmailVerified, "email_verified"); } if (AbpClaimTypes.PhoneNumber != "phone_number") { claimActions.MapJsonKey(AbpClaimTypes.PhoneNumber, "phone_number"); } if (AbpClaimTypes.PhoneNumberVerified != "phone_number_verified") { claimActions.MapJsonKey(AbpClaimTypes.PhoneNumberVerified, "phone_number_verified"); } if (AbpClaimTypes.Role != "role") { claimActions.MapJsonKeyMultiple(AbpClaimTypes.Role, "role"); } claimActions.RemoveDuplicate(AbpClaimTypes.Name); }
public static void RemoveDuplicate(this ClaimActionCollection claimActions, string claimType) { claimActions.Add(new RemoveDuplicateClaimAction(claimType)); }
public static void MapJsonKeyMultiple(this ClaimActionCollection claimActions, string claimType, string jsonKey) { claimActions.Add(new MultipleClaimAction(claimType, jsonKey)); }
private async Task <ClaimsPrincipal> ParseUserInformationAsync(HttpResponseMessage response, string claimsIssuer, ClaimActionCollection claimActions) { if (!response.IsSuccessStatusCode) { throw new HttpRequestException($"An error occurred when retrieving user information from {claimsIssuer}: ({response.StatusCode}). Please check if the authentication information is correct."); } var claimsIdentity = new ClaimsIdentity(claimsIssuer); var claimsPrincipal = new ClaimsPrincipal(claimsIdentity); using (var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync())) { foreach (var action in claimActions) { action.Run(payload.RootElement, claimsPrincipal.Identity as ClaimsIdentity, claimsIssuer); } } return(claimsPrincipal); }
public static void MapJsonKeyArray(this ClaimActionCollection collection, string claimType, string jsonKey) { collection.Add(new JsonKeyArrayClaimAction(claimType, null, jsonKey)); }
/// <summary> /// Clears any current ClaimsActions and maps all values from the json user data as claims, excluding the specified types. /// </summary> /// <param name="collection"></param> /// <param name="exclusions"></param> public static void MapAllExcept(this ClaimActionCollection collection, params string[] exclusions) { collection.MapAll(); collection.DeleteClaims(exclusions); }
/// <summary> /// Delete all claims from the given ClaimsIdentity with the given ClaimType. /// </summary> /// <param name="collection"></param> /// <param name="claimType"></param> public static void DeleteClaim(this ClaimActionCollection collection, string claimType) { collection.Add(new DeleteClaimAction(claimType)); }
/// <summary> /// Run the given resolver to select a value from the json user data to add as a claim. /// This no-ops if the returned value is empty. /// </summary> /// <param name="collection"></param> /// <param name="claimType">The value to use for Claim.Type when creating a Claim.</param> /// <param name="resolver">The Func that will be called to select value from the given json user data.</param> public static void MapCustomJson(this ClaimActionCollection collection, string claimType, Func <JsonElement, string> resolver) { collection.MapCustomJson(claimType, ClaimValueTypes.String, resolver); }
public static void MapBattleNetClaims(this ClaimActionCollection collection) { collection.MapJsonKey(JwtClaimTypes.Subject, JwtClaimTypes.Subject); collection.MapJsonKey(ExternalClaimTypes.BattleNet.Name, "battletag"); }
/// <summary> /// Selects a top level value from the json user data with the given key name and adds it as a Claim. /// This no-ops if the ClaimsIdentity already contains a Claim with the given ClaimType. /// This no-ops if the key is not found or the value is empty. /// </summary> /// <param name="collection"></param> /// <param name="claimType">The value to use for Claim.Type when creating a Claim.</param> /// <param name="jsonKey">The top level key to look for in the json user data.</param> public static void MapUniqueJsonKey(this ClaimActionCollection collection, string claimType, string jsonKey) { collection.MapUniqueJsonKey(claimType, jsonKey, ClaimValueTypes.String); }
/// <summary> /// Clears any current ClaimsActions and maps all values from the json user data as claims, excluding duplicates. /// </summary> /// <param name="collection"></param> public static void MapAll(this ClaimActionCollection collection) { collection.Clear(); collection.Add(new MapAllClaimsAction()); }
/// <summary> /// Run the given resolver to select a value from the json user data to add as a claim. /// This no-ops if the returned value is empty. /// </summary> /// <param name="collection"></param> /// <param name="claimType">The value to use for Claim.Type when creating a Claim.</param> /// <param name="valueType">The value to use for Claim.ValueType when creating a Claim.</param> /// <param name="resolver">The Func that will be called to select value from the given json user data.</param> public static void MapCustomJson(this ClaimActionCollection collection, string claimType, string valueType, Func <JsonElement, string> resolver) { collection.Add(new CustomJsonClaimAction(claimType, valueType, resolver)); }
/// <summary> /// Selects a top level value from the json user data with the given key name and adds it as a Claim. /// This no-ops if the ClaimsIdentity already contains a Claim with the given ClaimType. /// This no-ops if the key is not found or the value is empty. /// </summary> /// <param name="collection"></param> /// <param name="claimType">The value to use for Claim.Type when creating a Claim.</param> /// <param name="jsonKey">The top level key to look for in the json user data.</param> /// <param name="valueType">The value to use for Claim.ValueType when creating a Claim.</param> public static void MapUniqueJsonKey(this ClaimActionCollection collection, string claimType, string jsonKey, string valueType) { collection.Add(new UniqueJsonKeyClaimAction(claimType, valueType, jsonKey)); }
public static void MapDiscordClaims(this ClaimActionCollection collection) { collection.MapJsonKey(JwtClaimTypes.Subject, "id"); collection.MapJsonKey(ExternalClaimTypes.Discord.Name, "username"); collection.MapJsonKey(ExternalClaimTypes.Discord.Discriminator, "discriminator"); }