| | | 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 CoreWCF.IdentityModel.Claims; |
| | | 7 | | using CoreWCF.IdentityModel.Policy; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Primitives.Tests.CustomSecurity |
| | | 10 | | { |
| | | 11 | | public class MyTestAuthorizationPolicy : IAuthorizationPolicy |
| | | 12 | | { |
| | 4 | 13 | | public MyTestAuthorizationPolicy() |
| | | 14 | | { |
| | 4 | 15 | | Id = Guid.NewGuid().ToString(); |
| | 4 | 16 | | } |
| | | 17 | | |
| | | 18 | | public bool Evaluate(EvaluationContext evaluationContext, ref object state) |
| | | 19 | | { |
| | | 20 | | CustomAuthState customstate; |
| | | 21 | | |
| | | 22 | | // If the state is null, then this has not been called before so |
| | | 23 | | // set up a custom state. |
| | 4 | 24 | | if (state == null) |
| | | 25 | | { |
| | 4 | 26 | | customstate = new CustomAuthState(); |
| | 4 | 27 | | state = customstate; |
| | | 28 | | } |
| | | 29 | | else |
| | | 30 | | { |
| | 0 | 31 | | customstate = (CustomAuthState)state; |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | bool bRet; |
| | | 35 | | // If claims have not been added yet... |
| | 4 | 36 | | if (!customstate.ClaimsAdded) |
| | | 37 | | { |
| | | 38 | | // Create an empty list of claims. |
| | 4 | 39 | | IList<Claim> claims = new List<Claim> |
| | 4 | 40 | | { |
| | 4 | 41 | | new Claim("http://tempuri.org/claims/allowedoperation", "http://tempuri.org/IEchoService/EchoString" |
| | 4 | 42 | | new Claim("http://tempuri.org/claims/allowedoperation", "http://tempuri.org/IEchoService/ComplexEcho |
| | 4 | 43 | | }; |
| | 4 | 44 | | evaluationContext.AddClaimSet(this, new DefaultClaimSet(Issuer, claims)); |
| | | 45 | | // Record that claims were added. |
| | 4 | 46 | | customstate.ClaimsAdded = true; |
| | | 47 | | // Return true, indicating that this method does not need to be called again. |
| | 4 | 48 | | bRet = true; |
| | | 49 | | } |
| | | 50 | | else |
| | | 51 | | { |
| | | 52 | | // Should never get here, but just in case, return true. |
| | 0 | 53 | | bRet = true; |
| | | 54 | | } |
| | | 55 | | |
| | 4 | 56 | | return bRet; |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | public ClaimSet Issuer |
| | | 60 | | { |
| | 4 | 61 | | get { return ClaimSet.System; } |
| | | 62 | | } |
| | | 63 | | |
| | 4 | 64 | | public string Id { get; private set; } |
| | | 65 | | |
| | | 66 | | // Internal class for keeping track of state. |
| | | 67 | | private class CustomAuthState |
| | | 68 | | { |
| | 4 | 69 | | public CustomAuthState() |
| | | 70 | | { |
| | 4 | 71 | | ClaimsAdded = false; |
| | 4 | 72 | | } |
| | | 73 | | |
| | 12 | 74 | | public bool ClaimsAdded { get; set; } |
| | | 75 | | } |
| | | 76 | | } |
| | | 77 | | } |