C# code sample to validate bearer token for Outlook Actionable Messages
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.nuget
asp.net/ExpenseApprovalWebService
wcf/SurveyWebService
.gitignore
LICENSE
README.md
oam-web-service-samples.sln
outlook-actionable-messages-csharp-token-validation.yml

README.md

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) ||
            string.IsNullOrEmpty(request.Headers.Authorization.Parameter))
        {
            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 https://api.contoso.com with your service’s base URL.
        // For example, if the service target URL is https://api.xyz.com/finance/expense?id=1234,
        // then replace https://api.contoso.com with https://api.xyz.com
        ActionableMessageTokenValidationResult result = await validator.ValidateTokenAsync(bearerToken, "https://api.contoso.com");
        
        if (!result.ValidationSucceeded)
        {
            if (result.Exception != null)
            {
                Trace.TraceError(result.Exception.ToString());
            }

            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 expense@contoso.com (expected sender)
        // and the email of the person who performed the action is john@contoso.com (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, @"expense@contoso.com", StringComparison.OrdinalIgnoreCase) ||
            !string.Equals(result.ActionPerformer, @"john@contoso.com", 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.

Microsoft.O365.ActionableMessages.Utilities
System.IdentityModel.Tokens.Jwt

More information Outlook Actionable Messages is available here.

Copyright

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 opencode@microsoft.com with any additional questions or comments.