Skip to content

Instantly share code, notes, and snippets.

@anotherRedbeard
anotherRedbeard / gist:43e3ffe5d1f5a97438efc8febfe71bd4
Created December 8, 2023 17:30
Github repo for Policy Examples
https://github.com/Azure/api-management-policy-snippets/blob/master/README.md
@anotherRedbeard
anotherRedbeard / store-jwt-claim-value.xml
Created November 27, 2023 17:23
This is an example of how you could store the aud claim (or any other claim) into a context variable
<fragment>
<set-variable name="audClaim" value="@{
var authHeader = context.Request.Headers.GetValueOrDefault("Authorization");
if (!string.IsNullOrEmpty(authHeader)) {
var jwtToken = authHeader.AsJwt();
if (jwtToken != null) {
var claim = jwtToken.Claims.GetValueOrDefault("aud");
if (claim != null) {
return claim;
}
@anotherRedbeard
anotherRedbeard / validate-cert-policy.xml
Created November 17, 2023 16:27
Policy Fragment that will validate a certificate and send back specific 403 messages depending on what failed
<!--
IMPORTANT:
- Policy fragment are included as-is whenever they are referenced.
- If using variables. Ensure they are setup before use.
- Copy and paste your code here or simply start coding
Commenting out the verify logic as it doesn't work with self-signed certificates
-->
<fragment>
<choose>
@anotherRedbeard
anotherRedbeard / aadb2c-validate-jwt-policy.xml
Last active November 17, 2023 16:27
Policy Fragment that will allow you to validate an AADB2C token using at most 3 dynamic 'scp' claims.
<!--
IMPORTANT:
- Policy fragment are included as-is whenever they are referenced.
- If using variables. Ensure they are setup before use.
- Copy and paste your code here or simply start coding
-->
<fragment>
<validate-jwt header-name="Authorization" failed-validation-httpcode="401" failed-validation-error-message="Unauthorized. AADB2C Access token is missing or invalid.">
<openid-config url="https://{{aadb2c-tenant-name}}.b2clogin.com/{{aadb2c-tenant-name}}.onmicrosoft.com/{{aadb2c-signin-signup-name}}/v2.0/.well-known/openid-configuration" />
<audiences>
@anotherRedbeard
anotherRedbeard / ping-validate-jwt-policy.xml
Last active November 17, 2023 16:28
This APIM fragment will introspect the incoming token from the Authorization header. If a 200 code is returned, then validate the token with the regular <validate-jwt> policy, but if it's not valid return a 401. If something other than a 200 is returned, then return the response code and set the reason to 'APIM POlicy Detected Error'.
<!--
IMPORTANT:
- Policy fragment are included as-is whenever they are referenced.
- If using variables. Ensure they are setup before use.
- Copy and paste your code here or simply start coding
-->
<fragment>
<send-request mode="new" response-variable-name="introspectResponse" timeout="20" ignore-error="false">
<set-url>{{PingAuthURI}}as/introspect</set-url>
<set-method>POST</set-method>
@anotherRedbeard
anotherRedbeard / mermaid.md
Created May 31, 2023 22:45
Mermaid examples

::: mermaid flowchart LR subgraph aca[Azure Container Apps] subgraph app[Value Wire App] blazorApp[Blazor App] end subgraph api[Value Wire Api] webApi[dotnet Web Api] end end

@anotherRedbeard
anotherRedbeard / send-SB-message-to-broker-using-SAS.xml
Last active November 27, 2023 17:25
Azure API Management policy to create a Service Bus SAS token and send a SB message to the broker
<policies>
<inbound>
<base />
<rewrite-uri template="/" />
<cache-lookup-value key="sbToken" variable-name="cachedSasToken" />
<choose>
<when condition="@(context.Variables.GetValueOrDefault<string>("cachedSasToken") == null)">
<cache-store-value key="sbToken" value="@{
string resourceUri = "<service-bus-url>";
string keyName = "<access-policy-name>";
@anotherRedbeard
anotherRedbeard / center.css
Created November 11, 2020 15:55
CSS center things within a div
.parent {
position: relative;
}
.child {
width: 300px;
height: 100px;
padding: 20px;
position: absolute;
@anotherRedbeard
anotherRedbeard / fullTextSearch.txt
Created July 2, 2020 18:58
SO post on full text search for sql server
https://stackoverflow.com/questions/506034/converting-user-entered-search-query-to-where-clause-for-use-in-sql-server-full/30409654#30409654
@anotherRedbeard
anotherRedbeard / ReadCorruptedXRefPdf.cs
Created July 1, 2020 18:19
Workaround to fix PDFSharp error/bug of invalid entry in xref table.
PdfDocument documentInput;
using (var stream = new MemoryStream(new WebClient().DownloadData(pdfUrl)))
{
try
{
documentInput = PdfReader.Open(stream, PdfDocumentOpenMode.Import);
}
catch (PdfReaderException ex)
{
try