Coder Social home page Coder Social logo

vasanttiwari / outlook-actionable-messages-csharp-token-validation Goto Github PK

View Code? Open in Web Editor NEW

This project forked from officedev/outlook-actionable-messages-csharp-token-validation

0.0 0.0 0.0 2.96 MB

C# code sample to validate bearer token for Outlook Actionable Messages

License: MIT License

C# 40.56% CSS 0.67% ASP 0.03% HTML 1.29% JavaScript 57.45%

outlook-actionable-messages-csharp-token-validation's Introduction

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());
        }

        // Replace https://api.contoso.com with your service domain URL.
        // For example, if the service URL is https://api.xyz.com/finance/expense?id=1234,
        // then replace https://api.contoso.com with https://api.xyz.com
        string bearerToken = request.Headers.Authorization.Parameter;
        ActionableMessageTokenValidator validator = new ActionableMessageTokenValidator();
        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 verify the sender and the action performer. 
        // You should replace the code below with your own validation logic.
        // In this example, we verify that the email is sent by [email protected]
        // and the action performer has to be someone with @contoso.com email.
        //
        // 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, @"[email protected]", StringComparison.OrdinalIgnoreCase) ||
            !result.ActionPerformer.ToLower().EndsWith("@contoso.com"))
        {
            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.

outlook-actionable-messages-csharp-token-validation's People

Contributors

wowong avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.