question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Network] Example of setting Data property from X509Certificate2

See original GitHub issue

It wasn’t clear to me how to set the certificate Data property given an X509Certificate2. I spent several hours trying to figure this out ultimately cloning the repository to look at the code and examples/unit tests. This is how it’s done:

X509Certificate2 certificate;
...
string base64EncodedCertificate = Convert.ToBase64String(certificate.Export(X509ContentType.Cert));

new ApplicationGatewayTrustedClientCertificate
{
  Data = BinaryData.FromObjectAsJson(base64EncodedCertificate);
}

Nothing else I tried would work. IMO, this is not the most obvious usage. Other things I tried that don’t work but seem more sensible:

new ApplicationGatewayTrustedClientCertificate
{
  Data = BinaryData.FromBytes(certificate.Export(X509ContentType.Cert));
}

new ApplicationGatewayTrustedClientCertificate
{
  Data = BinaryData.FromString(Convert.ToBase64String(certificate.Export(X509ContentType.Cert)));
}

The documentation on how to set the BinaryData property isn’t very helpful unless you already know the answer. For setting certificate Data would be much more helpful to show an example based on an X509Certificate rather than a string “foo”.


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

Issue Analytics

  • State:closed
  • Created 6 months ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
live1206commented, Apr 7, 2023

@CodyBatt The reason why we expect the input of BinaryData as json is based on how we serialize it, we thought Data could be anything as configured in autorest.md

format-by-name-rules:
  'data': 'any'

But the type of Data is defined as string in Swagger. This is the similar to https://github.com/Azure/azure-sdk-for-net/issues/33804, we are looking into a proper fix for it.

cc @m-nash

0reactions
CodyBattcommented, Aug 14, 2023

Okay

Read more comments on GitHub >

github_iconTop Results From Across the Web

X509Certificate2 Class (System.Security.Cryptography. ...
The following example creates a command-line executable that takes a certificate file as an argument and prints various certificate properties to the console....
Read more >
X509Certificate2.RawData Property (System.Security. ...
The following code example creates a command-line executable that takes a certificate file as an argument and prints various certificate properties to the ......
Read more >
Associate a private key with the X509Certificate2 class in .net
net. I'm working on some code that creates a X509certificate and a public/private key pair. The public key is added to the certificate...
Read more >
Seven tips for working with X.509 certificates in .NET
Tip 1: Understand the difference between certificates and PKCS #12/PFX files. In .NET, the X509Certificate2 object has properties for the ...
Read more >
Accessing and using certificate private keys in .NET ...
An X509Certificate2 class has a PrivateKey property of AsymmetricAlgorithm type. AsymmetricAlgorithm class is abstract class for any asymmetric ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found