2017-05-31 3 views
1

JSONテキストをSecureString変数を含むオブジェクトにシリアル化する際に問題があります。パブリック 'pwd'変数をSecureStringのままにしておくと、次のようにシリアル化することはできますか?JSON ToObject with SecureString

のは、次の文字列があるとしましょう:だから私はJSON ToObjectを使用して、次の呼び出しをしてもよい

string j_str = "{ 'uid':'JohnDoe', 'age':30, 'pwd':'MyPassword' }"; 

を、私はC#で、私が持っているオブジェクトに

public class User 
{ 
    public string uid = { get; set; } 
    public string age = { get; set; } 
    public SecureString pwd = { get; set; } 
} 

をこの文字列をシリアル化したいと思います)Newtonsoftのように呼び出す:

JObject j_obj = JObject.Parse(j_str); 

User newUser = j_obj.ToObject<User>(); 

この結果、次のエラーが発生します。

Newtonsoft.Json.JsonSerializationException: 'Error converting value "MyPassword" to type 'System.Security.SecureString'. Path 'pwd' 
+0

変更 'pwd'は財産の種類STRING' –

+1

' _に "SecureString国民PWD '変数を維持しながら?" _ - それは[XY問題](HTTPSのようになります/ /meta.stackexchange.com/questions/66377/what-is-the-xy-problem)。なぜこれを最初にやりたいのですか? – CodeCaster

答えて

6

文字列からこれを構築することは、SecureStringを使用する背後にある原則に反しているようです。 https://msdn.microsoft.com/en-us/library/system.security.securestring(v=vs.110).aspxを参照してください:

A SecureString object should never be constructed from a String, because the sensitive data is already subject to the memory persistence consequences of the immutable String class. The best way to construct a SecureString object is from a character-at-a-time unmanaged source, such as the Console.ReadKey method.