Need help to build Azure API policy for Soap Ui to authenticate "Created <DateNow<Expires"

Kumar, Manish 0 Reputation points
2023-03-16T00:57:29.72+00:00

Need help to build Azure API police for Soap Ui to authenticate "Created Date<DataNow<Expires Date".

Policy Condition/Use case: - The code extracts the Created and Expires header values, captures date now, and then checks that Created<DataNow<Expires.

Can anyone help me with the code snippet.

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,447 questions
{count} votes

1 answer

Sort by: Most helpful
  1. MuthuKumaranMurugaachari-MSFT 22,441 Reputation points Moderator
    2023-03-16T20:13:09.3766667+00:00

    Kumar, Manish Based on your description of the use case, you need to extract the header or body using Context variable and then check for conditions after parsing it to System.DateTime object in your policy. Refer similar thread: https://learn.microsoft.com/en-us/answers/questions/1189885/how-to-check-whether-the-current-date-time-falls-w which has policy reference and supported types.

    For header input like this, try the below policy snippet:

    Sample request header:

    User's image

    Policy Snippet:

    <set-variable name="isDateWithinRange" value="@{
                                var createdDate = Convert.ToDateTime((String)context.Request.Headers.GetValueOrDefault("CreatedDate", ""));
                                var expiresDate = Convert.ToDateTime((String)context.Request.Headers.GetValueOrDefault("ExpiresDate", ""));
                                string isDateWithinRange = "No";
                                if(DateTime.UtcNow >= createdDate && DateTime.UtcNow <= expiresDate) 
                                {
                                    isDateWithinRange = "Yes";
                                }
                                return isDateWithinRange;
                            }" />
    

    For XML request body, use XDocument.Parse() method and we have a sample policy available here: Extracting multiple values from xml documents.policy.

    Sample request body:

    <?xml version="1.0" encoding="utf-8"?>
    <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    	<CreatedDate>2023-03-17T08:53:17.866+00:00</CreatedDate>
    	<ExpiresDate>2023-03-18T08:53:17.866+00:00</ExpiresDate>
    </Envelope>
    

    Policy Snippet:

    <set-variable name="isDateWithinRange" value="@{
                                var createdDate = Convert.ToDateTime((String)context.Request.Headers.GetValueOrDefault("CreatedDate", ""));
                                var expiresDate = Convert.ToDateTime((String)context.Request.Headers.GetValueOrDefault("ExpiresDate", ""));
                                string isDateWithinRange = "No";
                                if(DateTime.UtcNow >= createdDate && DateTime.UtcNow <= expiresDate) 
                                {
                                    isDateWithinRange = "Yes";
                                }
                                return isDateWithinRange;
                            }" />
    

    Note, the above policy snippet is just for reference, and you can customize it based on your need. I hope this helps and let me know if any questions.


    If you found the answer to your question helpful, please take a moment to mark it as "Yes" for others to benefit from your experience. Or simply add a comment tagging me and would be happy to answer your questions.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.