- Overview
- Prerequisites
- Step 1: Generate a .NET 4.5 API Client Library
- Step 2: Open and Build the Generated Solution
- Step 3: Add a Console App Project to the Solution
- Step 4: Extend ApiClient to Use JSON
- Step 5: Send Requests to the MDES Search API
Mastercard provides client libraries for integrating with its services, but the .NET packages don't support .NET Framework 4.5, whose support ended in 2016:
- The C# OAuth1 Signer lib supports .NET standard 1.3 (.NET Framework 4.6+)
- Mastercard Core and Mastercard MDES Customer Service SDKs support .NET Framework 4.6.1+
This tutorial explains how to consume the Mastercard MDES Customer Service from a .NET 4.5 app by generating an API client library with OpenAPI generator and signing HTTP requests using a .NET 4.5 version of the OAuth1 Signer lib.
- Read the MDES Customer Service documentation
- Download the MDES Customer Service OpenAPI specification
- Set up a project in the Mastercard Developers Portal, add the MDES Customer Service API to it:
- Checkout and build the .NET 4.5 version of the OAuth1 Signer library
A .NET 4.5 client library for MDES Customer Service can be generated using the following command:
java -jar openapi-generator-cli.jar generate -i mdes-customer-service-2.0.4.yaml -g csharp -c config.json -o mdes-customer-service-net45-tutorial
config.json:
{
"targetFramework": "v4.5",
"packageName": "YourApp.MdesCustomerClient"
}
See also: OpenAPI Generator (executable)
- Navigate to the mdes-customer-service-net45-tutorial/src folder
- Fix the project files (.csproj) by deleting
Condition="Exists('..\..\packages')"
inHintPath
elements, example:
<Reference Include="RestSharp">
<HintPath>..\..\packages\RestSharp.105.1.0\lib\net45\RestSharp.dll</HintPath>
</Reference>
- Open YourApp.MdesCustomerClient.sln
- Click Build > Rebuild Solution
- Click File > New > Project...
- Choose Console App (.NET Framework) and configure the project the following way:
- Right-click on References and add the following dependencies to the
YourApp.Console
project:YourApp.MdesCustomerClient
Mastercard.Developer.OAuth1Signer.Core.dll
(.NET 4.5 version)Mastercard.Developer.OAuth1Signer.RestSharp.dll
(.NET 4.5 version)RestSharp.dll
By default, the MDES Customer Service is accepting and returning XML payloads when our generated code expects JSON to be used.
To indicate the service we speak JSON, create a new class in the YourApp.MdesCustomerClient
project defining the partial method InterceptRequest
the following way:
using RestSharp;
namespace YourApp.MdesCustomerClient.Client
{
/// <summary>
/// Extends the generated ApiClient class.
/// </summary>
public partial class ApiClient
{
/// <summary>
/// Adds "Format=JSON" to the RestSharp request so that the service accepts and returns JSON instead of XML.
/// </summary>
partial void InterceptRequest(IRestRequest request) => request.AddQueryParameter("Format", "JSON");
}
}
Step 5: Send Requests to the MDES Search API
- Update Program.cs with the following code:
class Program
{
static void Main(string[] args)
{
// The request was aborted: Could not create SSL/TLS secure channel
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var consumerKey = "<insert consumer key>";
var signingKey = SecurityUtils.LoadPrivateKey("<insert PKCS#12 key file path>", "<insert key alias>", "<insert key password>"); // Pass X509KeyStoragFlags here
var config = Configuration.Default;
config.BasePath = "https://sandbox.api.mastercard.com/mdes/csapi/v2";
config.ApiClient.RestClient.Authenticator = new RestSharpOAuth1Authenticator(consumerKey, signingKey, new Uri(config.BasePath));
var searchApi = new SearchApi(config);
var auditInfo = new AuditInfo("A1435477", "John Smith", "Any Bank", "5555551234");
var tokenUniqueReference = "DWSPMC00000000010906a349d9ca4eb1a4d53e3c90a11d9c";
var searchRequest = new SearchRequest(null, tokenUniqueReference, null, null, null, null, null, null, auditInfo);
var response = searchApi.SearchPost(new SearchRequestSchema(searchRequest));
System.Console.WriteLine(response.SearchResponse.Accounts.Account[0].Tokens.Token[0]);
System.Console.ReadLine();
}
}
- Use your own
consumerKey
,pkcs12KeyFilePath
,signingKeyAlias
,signingKeyAlias
andkeyStorageFlags
. - Click Debug
- You should get a result similar to this one: