2012-05-30 5 views
2

사용자 정보를 안전한 방법으로 저장하여 자격 증명이 포함 된 클래스를 가져 와서 serialize하고 protectedData를 사용하여 암호화 한 다음이 새로운 암호화 된 데이터를 격리 된 위치에 저장합니다. 저장. 나는 내가에서 개체를 역 직렬화 할 때격리 된 저장소 및 ProtectedData를 사용하여 사용자 자격 증명 저장

public bool SaveCredentials(ILoginCredentials credentials) 
    { 
     try 
     { 
      //CredentialStorage implements ILoginCredentials 
      CredentialStorage storage = new CredentialStorage(credentials); 
      byte[] lastEncryptedData = ToByteArray(storage); 
      lastEncryptedData = ProtectedData.Protect(lastEncryptedData, AditionalEntropy, DataProtectionScope.CurrentUser); 

      IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null); 
      IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("ExternalSSOProvider", FileMode.Create, 
                       FileAccess.Write, isoStore); 
      isoStream.Write(lastEncryptedData, 0, lastEncryptedData.Length); 
      isoStream.Close(); 
      return true; 
     } 
     catch (Exception) 
     { 
      return false; 
     } 
    } 

    private static byte[] ToByteArray(object source) 
    { 
     var formatter = new BinaryFormatter(); 
     using (var stream = new MemoryStream()) 
     { 
      formatter.Serialize(stream, source); 
      return stream.ToArray(); 
     } 
    } 
코드의이 비트는 아무 문제 그리고

에게 작동하지 않을 것 같다

내가 가지고 복원 코드가

private CredentialStorage GetCredentials() 
    { 
     try 
     { 
      IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null); 
      if (isoStore.FileExists("ExternalSSOProvider")) 
      { 
       using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("ExternalSSOProvider", FileMode.Open, isoStore)) 
       { 
        using (StreamReader reader = new StreamReader(stream)) 
        { 
         using(MemoryStream ms = new MemoryStream()) 
         { 
          reader.BaseStream.CopyTo(ms); 
          byte[] protectedMemory = ms.ToArray(); 
          ms.Close(); 
          ProtectedData.Unprotect(protectedMemory, AditionalEntropy, DataProtectionScope.CurrentUser); 
          return ToCredentials(protectedMemory); 
         } 
        } 
       } 
      } 
     } 
     catch (Exception) 
     { 
      return null; 
     } 
     return null; 
    } 

    private static CredentialStorage ToCredentials(byte[] source) 
    { 
     var formatter = new BinaryFormatter(); 
     using (var stream = new MemoryStream(source)) 
     { 
      var x = formatter.Deserialize(stream); //My exception occurs here 
      return x as CredentialStorage; 
     } 
    } 

객체에 저장 방법 다음 한 ToCredentials 메서드 다음 오류가 발생합니다.

이진 스트림 'n'에 유효한 BinaryHeader가 없습니다. 직렬화와 비 직렬화간에 유효하지 않은 스트림 또는 오브젝트 버전이 변경 될 수 있습니다.

도움이 되었으면 감사드립니다.

은 참고로이

public interface ILoginCredentials 
{ 
    string Username { get; } 
    string Password { get; } 
} 
+1

복구 할 수있는 방식으로 암호를 저장하지 마십시오. 그들을 해쉬하고 해시를 저장하십시오. 나는 그것이 당신의 질문에 대답하지 않는다는 것을 알고 있습니다. –

+0

SaveCredentials 메소드에서 닫기 전에 플러시를 시도 했습니까? –

+0

예 나는 그 감사를 시도했다. 그러나 아직도 didnt 한 직업 – John

답변

1

가 좋아 내가 문제를 발견 ILoginCredentials 인터페이스를합니다. GetCredentials 방법에서 나는 내가 결코 내가 여전히 암호화 된 데이터

에서 역 직렬화하려고 시도했습니다 반환 값으로 protectedMemory 변수를 업데이트하지 않았다

protectedMemory = ProtectedData.Unprotect(protectedMemory, AditionalEntropy, DataProtectionScope.CurrentUser); 

Becuase이 변경되었습니다

ProtectedData.Unprotect(protectedMemory, AditionalEntropy, DataProtectionScope.CurrentUser); 

라인을했다