Forum Xamarin.Forms

Reading a pfx file

So right now my issue is that I cannot reference a pfx file that is in my project. I have tried accessing the file as an asset by adding it to the assets folder, and then setting its Build Action to AndroidAsset.
AssetManager assets = this.Assets;
using (StreamReader sr = new StreamReader(assets.Open("beaconator.pfx")))
content = sr.ReadToEnd();
I would run the following code snippet and get this error message: Object reference not set to an instance of an object.

Then I tried putting the pfx file in the reference folder, and setting the Build Action to EmbeddedResource.
var clientCert = new X509Certificate2("beaconator.pfx", "abhinav");
Trying this code snippet I got the following error: Could not find file "/beaconator.pfx".

I have been trying to figure this issue out for a while, and am not sure what else to try. Any help would be greatly appreciated.


  • What you have should work fine. Here is a test project that uses StreamReader and the AssestManager to read the generic file AboutAssets.txt that is created for all forms projects. Since this is Android specific code, I put it in the MainActivity.cs. But it could be put anywhere that contains Android specific code, like an Android renderer.

Sign In or Register to comment.