|
1 |
| -# Common policy expressions |
2 |
| -This cheat-sheet contains common policy expressions that are often used when authoring Azure API Management policies. |
3 |
| - |
4 |
| -## Interact with HTTP headers |
5 |
| - |
6 |
| -**Get HTTP header** |
7 |
| -```c# |
8 |
| -context.Request.Headers.GetValueOrDefault("header-name","optional-default-value") |
9 |
| -``` |
10 |
| -**Check HTTP header existence** |
11 |
| -```c# |
12 |
| -context.Request.Headers.ContainsKey("header-name") == true |
13 |
| -``` |
14 |
| -**Check if HTTP header has expected value** |
15 |
| -```c# |
16 |
| -context.Request.Headers.GetValueOrDefault("header-name", "").Equals("expected-header-value", StringComparison.OrdinalIgnoreCase) |
17 |
| -``` |
18 |
| -## Interact with URI parameters |
19 |
| - |
20 |
| -**Get URI parameter** |
21 |
| -```c# |
22 |
| -context.Request.MatchedParameters.GetValueOrDefault("parameter-name","optional-default-value") |
23 |
| -``` |
24 |
| -**Check URI parameter existence** |
25 |
| -```c# |
26 |
| -context.Request.MatchedParameters.ContainsKey("parameter-name") == true |
27 |
| -``` |
28 |
| -**Check if URI parameter has expected value** |
29 |
| -```c# |
30 |
| -context.Request.MatchedParameters.GetValueOrDefault("parameter-name", "").Equals("expected-value", StringComparison.OrdinalIgnoreCase) == true |
31 |
| -``` |
32 |
| -## Interact with query string parameters |
33 |
| - |
34 |
| -**Get query string parameter** |
35 |
| -```c# |
36 |
| -context.Request.Url.Query.GetValueOrDefault("parameter-name", "optional-default-value") |
37 |
| -``` |
38 |
| -**Check query string parameter existence** |
39 |
| -```c# |
40 |
| -context.Request.Url.Query.ContainsKey("parameter-name") == true |
41 |
| -``` |
42 |
| -**Check if query string parameter has expected value** |
43 |
| -```c# |
44 |
| -context.Request.Url.Query.GetValueOrDefault("parameter-name", "").Equals("expected-value", StringComparison.OrdinalIgnoreCase) == true |
45 |
| -``` |
46 |
| -## Interact with policy variables |
47 |
| - |
48 |
| -**Get policy variable** *(assuming type string)* |
49 |
| -```c# |
50 |
| -context.Variables.GetValueOrDefault<string>("variable-name","optional-default-value") |
51 |
| -``` |
52 |
| -**Check policy variable existence** |
53 |
| -```c# |
54 |
| -context.Variables.ContainsKey("variable-name") == true |
55 |
| -``` |
56 |
| -**Check if policy variable has expected value** *(assuming type string)* |
57 |
| -```c# |
58 |
| -context.Variables.GetValueOrDefault<string>("variable-name","").Equals("expected-value", StringComparison.OrdinalIgnoreCase) |
59 |
| -``` |
60 |
| -## Interact with JSON bodies |
61 |
| - |
62 |
| -**Get value from JSON body** |
63 |
| -```c# |
64 |
| -(string)context.Request.Body.As<JObject>(preserveContent: true).SelectToken("root.child jsonpath") |
65 |
| -``` |
66 |
| -**Get value from JSON response variable** |
67 |
| -```c# |
68 |
| -(string)((IResponse)context.Variables["response-variable-name"]).Body.As<JObject>().SelectToken("root.child jsonpath") |
69 |
| -``` |
70 |
| -**Add property to JSON body** |
71 |
| -```c# |
72 |
| -JObject body = context.Request.Body.As<JObject>(); |
73 |
| -body.Add(new JProperty("property-name", "property-value")); |
74 |
| -return body.ToString(); |
75 |
| -``` |
76 |
| -## Interact with JSON Web Tokens |
77 |
| - |
78 |
| -**Read claim from bearer token** |
79 |
| -```c# |
80 |
| -context.Request.Headers.GetValueOrDefault("Authorization")?.Split(' ')?[1].AsJwt()?.Claims["claim-name"].FirstOrDefault() |
81 |
| -``` |
82 |
| - |
83 |
| -## Interact with client certificates |
84 |
| - |
85 |
| -**Check client certificate existence** |
86 |
| -```c# |
87 |
| -context.Request.Certificate != null |
88 |
| -``` |
89 |
| -**Check if client certificate is valid, including a certificate revocation check** |
90 |
| -```c# |
91 |
| -context.Request.Certificate.Verify() == true |
92 |
| -``` |
93 |
| -**Check if client certificate is valid, excluding a certificate revocation check** |
94 |
| -```c# |
95 |
| -context.Request.Certificate.VerifyNoRevocation() == true |
96 |
| -``` |
97 |
| -**Check if client certificate issuer has expected value** |
98 |
| -```c# |
99 |
| -context.Request.Certificate.Issuer == "trusted-issuer" |
100 |
| -``` |
101 |
| -**Check if client certificate subject has expected value** |
102 |
| -```c# |
103 |
| -context.Request.Certificate.SubjectName.Name == "expected-subject-name" |
104 |
| -``` |
105 |
| -**Check if client certificate thumbprint has expected value** |
106 |
| -```c# |
107 |
| -context.Request.Certificate.Thumbprint == "EXPECTED-THUMBPRINT-IN-UPPER-CASE" |
108 |
| -``` |
109 |
| -**Check if client certificate is uploaded in API Management, based on thumbprint** |
110 |
| -```c# |
111 |
| -context.Deployment.Certificates.Any(c => c.Value.Thumbprint == context.Request.Certificate.Thumbprint) == true |
| 1 | +# Common policy expressions |
| 2 | +This cheat-sheet contains common policy expressions that are often used when authoring Azure API Management policies. |
| 3 | + |
| 4 | +## Interact with HTTP headers |
| 5 | + |
| 6 | +**Get HTTP header** |
| 7 | +```c# |
| 8 | +context.Request.Headers.GetValueOrDefault("header-name","optional-default-value") |
| 9 | +``` |
| 10 | +**Check HTTP header existence** |
| 11 | +```c# |
| 12 | +context.Request.Headers.ContainsKey("header-name") == true |
| 13 | +``` |
| 14 | +**Check if HTTP header has expected value** |
| 15 | +```c# |
| 16 | +context.Request.Headers.GetValueOrDefault("header-name", "").Equals("expected-header-value", StringComparison.OrdinalIgnoreCase) |
| 17 | +``` |
| 18 | +## Interact with URI parameters |
| 19 | + |
| 20 | +**Get URI parameter** |
| 21 | +```c# |
| 22 | +context.Request.MatchedParameters.GetValueOrDefault("parameter-name","optional-default-value") |
| 23 | +``` |
| 24 | +**Check URI parameter existence** |
| 25 | +```c# |
| 26 | +context.Request.MatchedParameters.ContainsKey("parameter-name") == true |
| 27 | +``` |
| 28 | +**Check if URI parameter has expected value** |
| 29 | +```c# |
| 30 | +context.Request.MatchedParameters.GetValueOrDefault("parameter-name", "").Equals("expected-value", StringComparison.OrdinalIgnoreCase) == true |
| 31 | +``` |
| 32 | +## Interact with query string parameters |
| 33 | + |
| 34 | +**Get query string parameter** |
| 35 | +```c# |
| 36 | +context.Request.Url.Query.GetValueOrDefault("parameter-name", "optional-default-value") |
| 37 | +``` |
| 38 | +**Check query string parameter existence** |
| 39 | +```c# |
| 40 | +context.Request.Url.Query.ContainsKey("parameter-name") == true |
| 41 | +``` |
| 42 | +**Check if query string parameter has expected value** |
| 43 | +```c# |
| 44 | +context.Request.Url.Query.GetValueOrDefault("parameter-name", "").Equals("expected-value", StringComparison.OrdinalIgnoreCase) == true |
| 45 | +``` |
| 46 | +## Interact with policy variables |
| 47 | + |
| 48 | +**Get policy variable** *(assuming type string)* |
| 49 | +```c# |
| 50 | +context.Variables.GetValueOrDefault<string>("variable-name","optional-default-value") |
| 51 | +``` |
| 52 | +**Check policy variable existence** |
| 53 | +```c# |
| 54 | +context.Variables.ContainsKey("variable-name") == true |
| 55 | +``` |
| 56 | +**Check if policy variable has expected value** *(assuming type string)* |
| 57 | +```c# |
| 58 | +context.Variables.GetValueOrDefault<string>("variable-name","").Equals("expected-value", StringComparison.OrdinalIgnoreCase) |
| 59 | +``` |
| 60 | +## Interact with JSON bodies |
| 61 | + |
| 62 | +**Get value from JSON body** |
| 63 | +```c# |
| 64 | +(string)context.Request.Body.As<JObject>(preserveContent: true).SelectToken("root.child jsonpath") |
| 65 | +``` |
| 66 | +**Get value from JSON response variable** |
| 67 | +```c# |
| 68 | +(string)((IResponse)context.Variables["response-variable-name"]).Body.As<JObject>().SelectToken("root.child jsonpath") |
| 69 | +``` |
| 70 | +**Add property to JSON body** |
| 71 | +```c# |
| 72 | +JObject body = context.Request.Body.As<JObject>(); |
| 73 | +body.Add(new JProperty("property-name", "property-value")); |
| 74 | +return body.ToString(); |
| 75 | +``` |
| 76 | +## Interact with JSON Web Tokens |
| 77 | + |
| 78 | +**Read claim from bearer token** |
| 79 | +```c# |
| 80 | +context.Request.Headers.GetValueOrDefault("Authorization")?.Split(' ')?[1].AsJwt()?.Claims["claim-name"].FirstOrDefault() |
| 81 | +``` |
| 82 | + |
| 83 | +## Interact with client certificates |
| 84 | + |
| 85 | +**Check client certificate existence** |
| 86 | +```c# |
| 87 | +context.Request.Certificate != null |
| 88 | +``` |
| 89 | +**Check if client certificate is valid, including a certificate revocation check** |
| 90 | +```c# |
| 91 | +context.Request.Certificate.Verify() == true |
| 92 | +``` |
| 93 | +**Check if client certificate is valid, excluding a certificate revocation check** |
| 94 | +```c# |
| 95 | +context.Request.Certificate.VerifyNoRevocation() == true |
| 96 | +``` |
| 97 | +**Check if client certificate issuer has expected value** |
| 98 | +```c# |
| 99 | +context.Request.Certificate.Issuer == "trusted-issuer" |
| 100 | +``` |
| 101 | +**Check if client certificate subject has expected value** |
| 102 | +```c# |
| 103 | +context.Request.Certificate.SubjectName.Name == "expected-subject-name" |
| 104 | +``` |
| 105 | +**Check if client certificate thumbprint has expected value** |
| 106 | +```c# |
| 107 | +context.Request.Certificate.Thumbprint == "EXPECTED-THUMBPRINT-IN-UPPER-CASE" |
| 108 | +``` |
| 109 | +**Check if client certificate is uploaded in API Management, based on thumbprint** |
| 110 | +```c# |
| 111 | +context.Deployment.Certificates.Any(c => c.Value.Thumbprint == context.Request.Certificate.Thumbprint) == true |
112 | 112 | ```
|
0 commit comments