internal static NativeCredential GetNativeCredential(Credential cred)
 {
     NativeCredential ncred = new NativeCredential();
     ncred.AttributeCount = 0;
     ncred.Attributes = IntPtr.Zero;
     ncred.Comment = IntPtr.Zero;
     ncred.TargetAlias = IntPtr.Zero;
     ncred.Type = CRED_TYPE.GENERIC;
     ncred.Persist = (UInt32)1;
     ncred.CredentialBlobSize = (UInt32)cred.CredentialBlobSize;
     ncred.TargetName = Marshal.StringToCoTaskMemUni(cred.TargetName);
     ncred.CredentialBlob = Marshal.StringToCoTaskMemUni(cred.CredentialBlob);
     ncred.UserName = Marshal.StringToCoTaskMemUni(Environment.UserName);
     return ncred;
 }
 public Credential GetCredential()
 {
     if (!IsInvalid)
     {
         NativeCredential ncred = (NativeCredential)Marshal.PtrToStructure(handle,
               typeof(NativeCredential));
         Credential cred = new Credential();
         cred.CredentialBlobSize = ncred.CredentialBlobSize;
         cred.CredentialBlob = Marshal.PtrToStringUni(ncred.CredentialBlob,
               (int)ncred.CredentialBlobSize / 2);
         cred.UserName = Marshal.PtrToStringUni(ncred.UserName);
         cred.TargetName = Marshal.PtrToStringUni(ncred.TargetName);
         cred.TargetAlias = Marshal.PtrToStringUni(ncred.TargetAlias);
         cred.Type = ncred.Type;
         cred.Flags = ncred.Flags;
         cred.Persist = ncred.Persist;
         return cred;
     }
     else
     {
         throw new InvalidOperationException("Invalid CriticalHandle!");
     }
 }