I’m getting an “Unable to obtain public key for StrongNameKeyPair.” exception using Newtonsoft’s JsonConvert.SerializeObject method in my asp.net application. It works locally but not on our test server. (if you know why, help me out at http://json.codeplex.com/workitem/22692) 🙂
I found Unable to obtain public key for StrongNameKeyPair and a few other posts that mentioned changing the permissions on the C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys directory, but is that secure and would my network administrator even let me do that?
EDIT: Here’s a stack trace:
3eUnable to obtain public key for StrongNameKeyPair.
System.ArgumentException
at System.Reflection.StrongNameKeyPair.ComputePublicKey()
at System.Reflection.StrongNameKeyPair.get_PublicKey()
at System.Reflection.Emit.AssemblyBuilder..ctor(AppDomain domain, AssemblyName name, AssemblyBuilderAccess access, String dir, Evidence evidence, PermissionSet requiredPermissions, PermissionSet optionalPermissions, PermissionSet refusedPermissions, StackCrawlMark& stackMark, IEnumerable1 unsafeAssemblyAttributes, SecurityContextSource securityContextSource)
1 unsafeAssemblyAttributes, SecurityContextSource securityContextSource)
at System.Reflection.Emit.AssemblyBuilder.InternalDefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access, String dir, Evidence evidence, PermissionSet requiredPermissions, PermissionSet optionalPermissions, PermissionSet refusedPermissions, StackCrawlMark& stackMark, IEnumerable
at System.AppDomain.InternalDefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access, String dir, Evidence evidence, PermissionSet requiredPermissions, PermissionSet optionalPermissions, PermissionSet refusedPermissions, StackCrawlMark& stackMark, IEnumerable`1 assemblyAttributes, SecurityContextSource securityContextSource)
at System.AppDomain.DefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access)
at Newtonsoft.Json.Utilities.DynamicWrapper.Init()
at Newtonsoft.Json.Utilities.DynamicWrapper.GenerateWrapperType(Type interfaceType, Type underlyingType)
at Newtonsoft.Json.Utilities.DynamicWrapper.GetWrapper(Type interfaceType, Type realObjectType)
at Newtonsoft.Json.Utilities.DynamicWrapper.CreateWrapper[T](Object realObject)
at Newtonsoft.Json.Converters.EntityKeyMemberConverter.WriteJson(JsonWriter writer, Object value, JsonSerializer serializer)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeConvertable(JsonWriter writer, JsonConverter converter, Object value, JsonContract contract, JsonContainerContract collectionContract, JsonProperty containerProperty)
……
I found this post: http://ayende.com/blog/1441/unable-to-obtain-public-key-for-strongnamekeypair and I’m thinking that if Ayende Rahien posted it, it’s a good answer.
I also only gave Everyone read and write permissions and it worked (the server may have been restarted, but I don’t know for sure if it was or if that helped).