| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Text; |
| | | 7 | | using CoreWCF.Description; |
| | | 8 | | using CoreWCF.Dispatcher; |
| | | 9 | | using CoreWCF.IdentityModel.Claims; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF |
| | | 12 | | { |
| | | 13 | | [AttributeUsage(CoreWCFAttributeTargets.OperationContract, Inherited = true)] |
| | | 14 | | public class AuthorizeRoleAttribute : Attribute, IAuthorizeOperation |
| | | 15 | | { |
| | | 16 | | private string[] _allowedRoles; |
| | | 17 | | |
| | 605 | 18 | | public AuthorizeRoleAttribute(params string[] allowedRoles) |
| | | 19 | | { |
| | 605 | 20 | | _allowedRoles = allowedRoles; |
| | 605 | 21 | | } |
| | | 22 | | |
| | | 23 | | public void BuildClaim(OperationDescription operationDescription, DispatchOperation dispatchOperation) |
| | | 24 | | { |
| | 158 | 25 | | if (operationDescription == null) |
| | | 26 | | { |
| | 0 | 27 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(operationDescription)); |
| | | 28 | | } |
| | | 29 | | |
| | 158 | 30 | | if (dispatchOperation == null) |
| | | 31 | | { |
| | 0 | 32 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(dispatchOperation)); |
| | | 33 | | } |
| | 158 | 34 | | if (_allowedRoles == null || _allowedRoles.Length ==0) |
| | | 35 | | { |
| | 0 | 36 | | return; |
| | | 37 | | } |
| | 158 | 38 | | List<Claim> allClaims = new List<Claim>(); |
| | 790 | 39 | | foreach(string role in _allowedRoles) |
| | | 40 | | { |
| | 237 | 41 | | allClaims.Add(new Claim(ClaimTypes.Role, role.Trim(), Rights.Identity)); |
| | | 42 | | } |
| | 158 | 43 | | if(allClaims.Count > 0) |
| | | 44 | | { |
| | 158 | 45 | | dispatchOperation.AuthorizeClaims.TryAdd(nameof(AuthorizeRoleAttribute), allClaims); |
| | | 46 | | } |
| | | 47 | | |
| | 158 | 48 | | } |
| | | 49 | | } |
| | | 50 | | } |