1
votes

Task and expected outcome

I have the following outbound policies in Azure API Management. The first removes a header if it exists and the second returns the Certificate expiration date/time (which I was using for testing).

Problem

There were several headers being removed initially, but I found that some of my custom outbound headers were disappearing.

It seems to be the policy immediately following the delete action that is being skipped. I have tried changing the order and the issue persists. I have also tried changing the delete action by adding a separate closing tag instead of self-closing, but that didn't work either.

I also removed and recreated the API from API Management and the issue remained.

Code and question

Is this due to a syntax error in my code?

    <outbound>
        <base />
        <set-header name="x-ms-workflow-name" exists-action="delete" />
        <set-header name="Certificate-Expiration" exists-action="override">
            <value>@{
                    var response = context.Request.Certificate.NotAfter;
                    return response.ToString();
                  }</value>
        </set-header>
        <set-header name="Certificate-Thumbprint" exists-action="override">
            <value>@{
                    var response = context.Request.Certificate.Thumbprint;
                    return response.ToString();
                  }</value>
        </set-header>
        <set-header name="Certificate-Subject" exists-action="override">
            <value>@{
                    var response = context.Request.Certificate.PublicKey.Key.ToXmlString(false);
                    return response.ToString();
                  }</value>
        </set-header>
    </outbound>
1

1 Answers

1
votes

I test it in my side, the policy <set-header> with exists-action="delete" and exists-action="override" work fine. The screenshot below is what I test in my side: enter image description here

Your problem was caused by the code var response = context.Request.Certificate.NotAfter, var response = context.Request.Certificate.Thumbprint, var response = context.Request.Certificate.PublicKey.Key.ToXmlString(false);. You need to check if context.Request.Certificate has NotAfter, Thumbprint and PublicKey first, then you can use them.

For example, when you want to use var response = context.Request.Certificate.Thumbprint;, you need to check if Thumbprint exists. You can write the code like: enter image description here