3

I'm having a Certificate under root folder of the project. The Project name is SingleSignOn, but I can't able to read the method using the GetManifestResourceStream build-in method.

The Source Code is

namespace SingleSignOn
{
    public class Program
    {
        static void Main(string[] args)
         {
            var assembly = typeof(Program).Assembly;
            var super = assembly.GetManifestResourceNames();
            using (var stream = assembly.GetManifestResourceStream("SingleSignOn.idsrv3test.pfx"))
            {

            }
        }
    }
}

Snap shot of Solution Explorer enter image description here

I'm getting NULL from the said built-in method GetManifestResourceStream

enter image description here

I don't know what I missed in this. The URL of the said Certificate is https://github.com/IdentityServer/IdentityServer3.Samples/blob/master/source/Certificates/idsrv3test.pfx

Kindly assist me how to read the Certificate.

B.Balamanigandan
  • 4,713
  • 11
  • 68
  • 130
  • 1
    right click the pfx file -> properties -> build action, make it 'embedded resource' – hyankov Mar 07 '17 at 08:40
  • why do you want to read that key? .net signing is automatically done. – Lei Yang Mar 07 '17 at 08:42
  • What is the default namespace of that assembly (namespace in project properties)? Because that is what used by GetManifestResourceStream to locate resource. – Evk Mar 07 '17 at 08:49
  • Guessing correctly at the assembly and the resource stream name is generally a perilous affair. Which is why you can use Project > Properties > Resources to avoid having to write code like this. You can double-check the stream name with a decompiler like ildasm.exe. Double-click the manifest to see the .mresource names. – Hans Passant Mar 07 '17 at 09:00
  • 1
    @HansPassant isn't resource name always DefaultNamespace.Folder.SubFolder.File? – Evk Mar 07 '17 at 09:24
  • Usually. Never for SO users however. They never tell us what they did wrong. – Hans Passant Mar 07 '17 at 09:44

4 Answers4

7

Try this:

var resourceName = "SingleSignOn.idsrv3test.pfx";
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
{
  TextReader tr = new StreamReader(stream);
  string fileContents = tr.ReadToEnd();
}

Note: Put the file as an embedded resource. file > right click > properties > build action > select embedded resource.

fatihyildizhan
  • 8,614
  • 7
  • 64
  • 88
Just code
  • 13,553
  • 10
  • 51
  • 93
3

Solution 2

Step 1: Change properties of .PFX file idsrv3test.pfx properties, set Build action as Embedded Resource.

Step 2: Code change:

var resourceName = "SingleSignOn.idsrv3test.pfx";
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
{
  TextReader tr = new StreamReader(stream);
  string fileContents = tr.ReadToEnd();
}
fatihyildizhan
  • 8,614
  • 7
  • 64
  • 88
2

Make sure that the file is an 'Embedded Resource'. Right click the pfx file -> Properties -> Build Action, make it 'embedded resource'

hyankov
  • 4,049
  • 1
  • 29
  • 46
1

My personal approach is to write an extension for the Assembly class, because this seems like a method that should have been included in that class anyway.

So, as mentioned by other posters, first make sure your text file is marked as "Embedded Resource", then use code similar to the following:

public static class Extensions
{
    public static string ReadTextResource(this Assembly asm, string resName)
    {
        string text;
        using (Stream strm = asm.GetManifestResourceStream(resName))
        {
            using (StreamReader sr = new StreamReader(strm))
            {
                text = sr.ReadToEnd();
            }
        }
        return text;
    }
}

(The above could be coded more succinctly, but I went with this for the purposes of demonstration)

jrichview
  • 338
  • 2
  • 14