Using CA Certificate to execute Anaplan REST API 2.0 from VB .Net (C#)
Hello,
we had an application that calls the REST API 2.0 using Basic Auth. Now want to move to CA Certificate approach to get the Auth Token but we get 401 error.
we took as a reference the instructions in link below:
we have the crt and key files.
has anyone done it in .net?
Thanks!
Comments
-
Hello,
The Pyhon code is in the doc but I was looking more for the VB .net code.
Just to make sure i'm doing in the right way
Thanks
0 -
Hi,
I got it working. Let me share some hints:
'Read CRT file content
Dim certStringContent As String = {File .crt content}'Authentication header -> Authorization: CACertificate {your_CA_certificate}
'{you_CA_certificate} should be replaced by the contents of the public key (crt file)
'This should include the contents between the "--- BEGIN CERTIFICATE ---" And "--- END CERTIFICATE ---" lines And Not including them
authHeader = String.Format("CACertificate {0}", certStringContent.Replace(vbCr, "").Replace(vbLf, "").Replace("-----BEGIN CERTIFICATE-----", "").Replace("-----END CERTIFICATE-----", ""))
'Add header to list of headers
listHeaders.Add(New KeyValuePair(Of String, String)("Authorization", authHeader))'*********************************************************
'Create HTTP content for Cert Authentication (Get Token)
'*********************************************************
'{
' "encodedData" : "{encodedString}",
' "encodedSignedData" : "{signedString}"
'}
' {encodedString} value should be a randomly generated base-64 encoded string of at least 100 bytes
' {signedString} value is the {encodedString} value that has been signed by the private key and then base-64 encoded.'Create a UnicodeEncoder to convert between byte array and string.
Dim ByteConverter As New ASCIIEncoding'--------------------------------------------------------------------------------------
' Create Encoded String {encodedString}
' 1. Create Random String (100 bytes)
' 2. Encode With base64.
'--------------------------------------------------------------------------------------'Create randmon string (This String will be signed By the Private key -> {signedString})
Dim randomData As String = RandomString(100)
'Encode random string with base64
Dim encodedData As String = Convert.ToBase64String(ByteConverter.GetBytes(randomData))'Read Private key file content from dashboard parameters
Dim privKeyStringContent As String = {File .key content}
'--------------------------------------------------------------------------------------
' Create Encoded Signed String {signedString}
' 1. Sign the original random string
' 2. Encode With base64.
'--------------------------------------------------------------------------------------'Create byte arrays to hold original and signed data
Dim originalData As Byte() = ByteConverter.GetBytes(randomData)
Dim signedData As Byte()'Create a RSA Crypto
Dim rsaAlg As New RSACryptoServiceProvider
'Private key in XML format
Dim privKey As String = BRApi.Dashboards.Parameters.GetLiteralParameterValue(si, False, "prm_anaplan_privkey")'Read private key from XML string
rsaAlg.FromXmlString(privKey)'Sign the original data (SHA512)
signedData = rsaAlg.SignData(originalData, SHA512.Create())'Encode the signed data with base64
Dim encodedSignedData As String = Convert.ToBase64String(signedData)'Build the JSON Body to get the token
Dim jsonBodyAuth = New Dictionary(Of String, String) From
{
{"encodedData", encodedData},
{"encodedSignedData", encodedSignedData}
}
'Serialize Json body (dictionary) as string
Dim jsonAuthContent As String = JsonConvert.SerializeObject(jsonBodyAuth, Formatting.Indented)
httpContent = New Http.StringContent(jsonAuthContent, Encoding.UTF8, "application/json")0 -
Hello, I'm trying to convert your code to c# and don't undestand this BRApi.Dashboards.Parameters.GetLiteralParameterValue(si, False, "prm_anaplan_privkey").
Which API did you use? How did you get anaplan key in XML format? I have private key in text format, how can I use it?
0 -
Yes,
forget about that one. That's an internal API of our software to read the private key from a parameter. The key is in the same format as you show so you can read it from a file
0 -
Ok and how does this API generate XML? What is the source? Private key is in text format.
0 -
I found the solution, I used https://superdry.apphb.com/tools/online-rsa-key-converter online service to convert PEM key to XML format. Thank you for VB idea.
0 -
I went through this nightmare getting things working with .net core 3.1. So for anyone who is interested and experiencing a similar challenge, i've created a gist you can all reference here:
https://gist.github.com/dbardouh/5e23ce80a6b62830c4dc8445316b18d6
This method will return the authentication token you can use in subsequent API requests in Anaplan.
I hope this helps someone.
0
Categories
- All Categories
- 2.3K Anaplan Community
- Academy
- Anaplan Talent Builder
- Model Design Course
- The Anaplan Way
- Archive
- 2 Idea exchange
- 62 Enterprise Scale
- 1.1K Extensibility
- 21 Intelligence
- 1.6K Planning & Modeling
- 331 Security
- Community Connections
- Connections
- Experiences
- Groups
- Personas
- Employees
- CS Toolkit
- Customer Care Center
- Forums
- Academy & Training
- Community Feedback & Updates
- Japan
- Anaplan Community Japan
- Anaplan Community Japan Knowledge Base
- HyperCare Japan
- JP-Central
- Support-Japanese
- Partners
- Partner Leadership Council
- Partner Product Council
- 724 Platform
- Anapedia
- App Hub
- Centers Of Excellence
- Extensions
- Planual
- Platform Updates
- 724 User Experience
- Profile Builder
- Resources
- Anaplan Advocates
- Anaplan Live!
- Community
- Community Advancement
- Community Connections
- Partner Program
- The Official Master Anaplanner Program
- Videos
- Welcome to the Anaplan Community!
- Success Central
- Support
- Case Portal Link
- Common Support Questions
- HyperCare Redirect
- Known Issues and Workarounds
- Support test page
- SupportFAQ
- Survey
- 2 Training Day Takeaways