Action Request Token Verification C# Sample

Services can send actionable messages to users to complete simple tasks against their services. When a user performs one of the actions in a message, an action request will be sent by Microsoft to the service. The request from Microsoft will contain a bearer token in the authorization header. This code sample shows how to verify the token to ensure the action request is from Microsoft, and use the claims in the token to validate the request.

    public async Task<HttpResponseMessage> Post([FromBody]string value)
        HttpRequestMessage request = this.ActionContext.Request;

        // Validate that we have a bearer token.
        if (request.Headers.Authorization == null ||
            !string.Equals(request.Headers.Authorization.Scheme, "bearer", StringComparison.OrdinalIgnoreCase) ||
            return request.CreateErrorResponse(HttpStatusCode.Unauthorized, new HttpError());
        // Get the token from the Authorization header 
        string bearerToken = request.Headers.Authorization.Parameter;
        ActionableMessageTokenValidator validator = new ActionableMessageTokenValidator();
        // This will validate that the token has been issued by Microsoft for the
        // specified target URL i.e. the target matches the intended audience (“aud” claim in token)
        // In your code, replace with your service’s base URL.
        // For example, if the service target URL is,
        // then replace with
        ActionableMessageTokenValidationResult result = await validator.ValidateTokenAsync(bearerToken, "");
        if (!result.ValidationSucceeded)
            if (result.Exception != null)

            return request.CreateErrorResponse(HttpStatusCode.Unauthorized, new HttpError());

        // We have a valid token. We will now verify that the sender and action performer are who
        // we expect. The sender is the identity of the entity that initially sent the Actionable 
        // Message, and the action performer is the identity of the user who actually 
        // took the action (“sub” claim in token). 
        // You should replace the code below with your own validation logic 
        // In this example, we verify that the email is sent by (expected sender)
        // and the email of the person who performed the action is (expected recipient)
        // You should also return the CARD-ACTION-STATUS header in the response.
        // The value of the header will be displayed to the user.
        if (!string.Equals(result.Sender, @"", StringComparison.OrdinalIgnoreCase) ||
            !string.Equals(result.ActionPerformer, @"", StringComparison.OrdinalIgnoreCase))
            HttpResponseMessage errorResponse = request.CreateErrorResponse(HttpStatusCode.Forbidden, new HttpError());
            errorResponse.Headers.Add("CARD-ACTION-STATUS", "Invalid sender or the action performer is not allowed.");
            return errorResponse;

        // Further business logic code here to process the expense report.

        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);
        response.Headers.Add("CARD-ACTION-STATUS", "The expense was approved.");
        return response;

The code sample is using the following libraries for JWT validation.


More information Outlook Actionable Messages is available here.

Copyright (c) 2017 Microsoft. All rights reserved.

This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact with any additional questions or comments.