C# SDK Quick Links

C# ASP.NET SDK

VerifyAndReadJwt(jwt, secret)

Verifies the signature of a JSON Web Token. If successful, it returns the instance id. You will use this for verifying and reading the JWT that was provided in the query string of the app component iframe. If you use the Client SDK to do the call to your server, the render JWT will be sent in the request headers.

Parameters:

Name Type Description
jwt string JSON Web Token you want to verify and read.
secret string The render secret, which can be found in the Developer Portal

Example:

VerifyAndReadJwt() example

var renderJwt = "";
var values = Request.Headers["instance-jwt"];

if (values.Count > 0)
{
    renderJwt = values.FirstOrDefault();
}
return renderJwt;

var renderSecret = "examplekey";

var instanceId = JwtHelper.VerifyAndReadJwt(renderJwt, renderSecret);

GenerateJwt(instanceId, secret)

Generates a JSON Web Token with the instance id in the payload and a lifetime of one hour.

Parameters:

Name Type Description
instanceId string The instance id that was read from the render JWT.
secret string The API secret, which can be found in the Developer Portal.

Example:

GenerateJwt() example

var apiSecret = "exampleApiKey";


// We use the instance id from the VerifyAndReadJwt() example
var apiJwt = JwtHelper.GenerateJwt(instanceId, apiSecret);

NOTE: Please refer to JWT.IO to find coding samples to generate a JWT outside of AppMarket SDK in different programming languages. You can also refer to this link to see one such example in groovy.

PostContactAsync(jwt, contact)

Post contact.

Parameters:

Name Type Description
jwt string JSON Web Token with instanceId as payload.
contact string JSON serialized contact object.

Example:

PostContactAsync() example

// If you handle the request body as an object, you have to convert it to a JSON string
var contactString = ContactsHelper.SerializeObject(contact);


// We use the apiJwt from the GenerateJwt() example
await sdk.PostContactAsync(apiJwt, contactString);

GetContactsAsync(jwt, queryString)

Get contacts.

Parameters:

Name Type Description
jwt string The API JWT.
queryString string A string containing all the query parameters. (see example)

Example:

GetContactsAsync()

// Here we insert the query values we got through our HttpGet method in a string
var parameters = $"scope={scope}&offset={offset}&limit={limit}";


// We use the apiJwt from the GenerateJwt() example
var contacts = await ContactsHelper.GetContactsAsync(apiJwt, parameters);

.Net Core 2.0 Web API: example using the SDK

using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using AppMarketServerSDK;
using Microsoft.AspNetCore.Cors;
using Newtonsoft.Json;

namespace ServerSdkTestApi.Controllers
{
    [Route("api")]
    [EnableCors("AllowAllOrigins")]
    public class ApiController : Controller
    {
        [HttpGet]
        [Route("contacts/get")]
        public async Task<object> GetContactsAsync(
            [FromQuery] string scope = "site",
            [FromQuery] string offset = null,
            [FromQuery] int limit = 50)
        {
            var parameters = $"scope={scope}&offset={offset}&limit={limit}";

            var instanceJwt = GetInstanceJwtFromHeaders();
            var instance = VerifyJwtToken(instanceJwt);
            var newJwt = GenerateJwtToken(instance);

            var result = await ContactsHelper.GetContactsAsync(newJwt, parameters);

            return result ?? BadRequest();
        }

        [HttpPost]
        [Route("contacts/post")]
        public async Task<IActionResult> PostContactAsync([FromBody] object contact)
        {
            var contactString = JsonConvert.SerializeObject(contact);

            var instanceJwt = GetInstanceJwtFromHeaders();
            var instance = VerifyJwtToken(instanceJwt);
            var newJwt = GenerateJwtToken(instance);

            try
            {
                await ContactsHelper.PostContactAsync(newJwt, contactString);
                return Ok();
            }
            catch (Exception ex)
            {
                return BadRequest(ex);
            }
        }

        public string GetInstanceJwtFromHeaders()
        {
            var instanceJwt = "";
            var values = Request.Headers["instance-jwt"];

            if (values.Count > 0)
            {
                instanceJwt = values.FirstOrDefault();
            }

            return instanceJwt;
        }

        public string GenerateJwtToken(string instance)
        {
            const string apiSecret = "exampleApiKey";

            var jwt = JwtHelper.GenerateJwt(instance, apiSecret);

            return jwt;
        }

        public string VerifyJwtToken(string token)
        {
            const string renderSecret = "exampleRenderKey";

            var instance = JwtHelper.VerifyAndReadJwt(token, renderSecret);

            return instance;
        }
    }
}