[HOW TO FIX] Azure APIM using context.User.Email causing request to fail

Manuprasad M N 40 Reputation points
2023-07-26T12:48:44.5333333+00:00

I want to add a the email id of the user in the header of the request which is being send from API Management service.

I have added the following policy in the inbound processing.

<set-header name="user" exists-action="override">   
         <value>@{var usr = context.User; return usr.Email;}</value> 
 </set-header>

Getting the following error in the trace

{     "messages": [
         {        
    	 "message": "Expression evaluation failed.",
       	"expression": "var usr = context.User; return usr.Email;",
		"details": "Object reference not set to an instance of an object." 
       	},

 		"Expression evaluation failed. Object reference not set to an instance of an object.", 
	"Object reference not set to an instance of an object." 
 		 ] 
}

I have tired another way also

<set-header name="user" exists-action="override">
            <value>@(context.User.Email)</value>
 </set-header>

Getting the following error in the trace

{
    "messages": [
        {
            "message": "Expression evaluation failed.",
            "expression": "context.User.Email",
            "details": "Object reference not set to an instance of an object."
        },
        "Expression evaluation failed. Object reference not set to an instance of an object.",
        "Object reference not set to an instance of an object."
    ]
}
Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,447 questions
{count} votes

Accepted answer
  1. MuthuKumaranMurugaachari-MSFT 22,441 Reputation points Moderator
    2023-07-26T14:13:07.0466667+00:00

    Manuprasad M N Thanks for posting your question in Microsoft Q&A. Based on the expression evaluation, it appears that context.User is null, and this might occur if the subscription key used in the request didn't have userId. Are you observing this error from Azure Portal Test console? If so, it uses built-in all-access subscription which doesn't have user info.

    User's image

    You can test with a subscription key which has user info mapped (like All APIs in the screenshot above) by passing it in the header like below and should provide the result.

    User's image

    Also, I would suggest checking context.User as null to avoid null reference exception. I hope this helps and let me know if any questions.

    set-header name="user" exists-action="override">
                <value>@(context.User?.Email)</value>
    </set-header>
    

    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

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.