Skip to main content
lesavage
Inspiring
February 27, 2017
Question

Can I use C#?

  • February 27, 2017
  • 1 reply
  • 595 views

The documentation details connecting with UMAPI using Python (Samples) however here is a C# sample for reference...

Generating a JSON Web Token (JWT):

using System;

using System.Collections.Generic;
using Jose;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography;

// the namespace would be the name of your project, mine is "aaa"
namespace aaa    
{
     class MainClass
     {
         public static void Main (string[] args)
         {

            DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
             TimeSpan diff = DateTime.Now.ToUniversalTime() - origin;
             var span =  Math.Floor(diff.TotalSeconds) + 60*60*24;

                                                                                             
             var payload = new Dictionary<string, object>()
             {
                 {"iss", “enter-yours"},
                 {"aud" , "enter-yours"},
                 {"exp", span},
                 {"sub", "enter-yours"},
                 {"https://ims-na1.adobelogin.com/s/ent_user_sdk", true}
             };

             var privateKey=new X509Certificate2("my-key.p12", “xxxxxx", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet).PrivateKey as RSACryptoServiceProvider;

             string token=Jose.JWT.Encode(payload, privateKey, JwsAlgorithm.RS256);
             Console.WriteLine (token);
         }
     }

Using the JWT to retrieve an Access Token:

using System;
using RestSharp;

namespace project2
{
     class MainClass
     {
         public static void Main(string[] args)
         {
             var client = new RestClient("https://ims-na1.adobelogin.com/ims/exchange/jwt"); // replace with production if necessary
             var request = new RestRequest(Method.POST);

             request.AddHeader("cache-control", "no-cache");
             request.AddHeader("content-type", "application/x-www-form-urlencoded");
            

            request.AddParameter("client_id", "your client id");
             request.AddParameter("client_secret", "your client secret");
             request.AddParameter("jwt_token", "your jwt token");

             IRestResponse response = client.Execute(request);
             Console.WriteLine(response.Content);
         }
     }
}

This topic has been closed for replies.

1 reply

Participant
September 25, 2017

I have a self-signed certificate. The code to get the JWT above fails for me with the following error: Is there an alternative method call to get the private key?

RsaUsingSha alg expects key to be of AsymmetricAlgorithm type

real_hoetz
Participant
August 17, 2018

Here's a more complete example in F# including a sample call to get a user's details:

GitHub - hoetz/AdobeUMAPI.FSharp