Exemplo n.º 1
0
        /// <summary>
        /// マネージドな資格情報をアンマネージドなポインタに変換する
        /// </summary>
        /// <param name="managedCred"></param>
        /// <returns></returns>
        private static UnmanagedCredential ConvertToUnmanagedCredential(ManagedCredential managedCred)
        {
            var unmanagedCred = new UnmanagedCredential()
            {
                Flags              = (uint)managedCred.Flags,
                Type               = (uint)managedCred.Type,
                TargetName         = managedCred.TargetName,
                Comment            = managedCred.Comment,
                LastWritten        = managedCred.LastWritten,
                CredentialBlobSize = managedCred.CredentialBlob != null ? (uint)managedCred.CredentialBlob.Length : 0,
                Persist            = (uint)managedCred.Persist,
                TargetAlias        = managedCred.TargetAlias,
                UserName           = managedCred.UserName,
                AttributeCount     = managedCred.Attributes != null ? (uint)managedCred.Attributes.Length : 0,
                Attributes         = IntPtr.Zero,
                CredentialBlob     = IntPtr.Zero,
            };


            if (unmanagedCred.AttributeCount != 0)
            {
                Marshal.Copy(managedCred.Attributes.Cast <int>().ToArray(), 0, unmanagedCred.Attributes, managedCred.Attributes.Length);
            }

            if (unmanagedCred.CredentialBlobSize != 0)
            {
                var p = Marshal.AllocHGlobal(managedCred.CredentialBlob.Length);
                Marshal.Copy(managedCred.CredentialBlob, 0, p, managedCred.CredentialBlob.Length);
                unmanagedCred.CredentialBlob = p;
                Marshal.FreeHGlobal(p);
            }

            return(unmanagedCred);
        }
Exemplo n.º 2
0
        /// <summary>
        /// アンマネージドな資格情報(ポインタ)をマネージドなEnumに変換する
        /// </summary>
        /// <param name="ptr"></param>
        /// <returns></returns>
        private static ManagedCredential ConvertToManagedCredential(IntPtr ptr)
        {
            var unmanagedCred = (UnmanagedCredential)Marshal.PtrToStructure(ptr, typeof(UnmanagedCredential));


            // 特殊な操作が必要ないものを詰め込んだManagedCredentialを作成する
            // CredentialBlobは別途変換する必要があるため、一旦サイズだけを持った配列を作成する
            var managedCred = new ManagedCredential
            {
                Flags          = (CRED_FLAGS)unmanagedCred.Flags,
                Type           = (CRED_TYPE)unmanagedCred.Type,
                TargetName     = unmanagedCred.TargetName,
                Comment        = unmanagedCred.Comment,
                LastWritten    = unmanagedCred.LastWritten,
                CredentialBlob = new byte[unmanagedCred.CredentialBlobSize],
                Persist        = (CRED_PERSIST)unmanagedCred.Persist,
                Attributes     = Enumerable.Range(0, (int)unmanagedCred.AttributeCount)
                                 .Select(x => Marshal.ReadIntPtr(unmanagedCred.Attributes, x * Marshal.SizeOf(typeof(CREDENTIAL_ATTRIBUTE))))
                                 .Select(x => (CREDENTIAL_ATTRIBUTE)Marshal.PtrToStructure(x, typeof(CREDENTIAL_ATTRIBUTE)))
                                 .ToArray(),
                TargetAlias = unmanagedCred.TargetAlias,
                UserName    = unmanagedCred.UserName,
            };

            if (unmanagedCred.CredentialBlobSize != 0)
            {
                Marshal.Copy(unmanagedCred.CredentialBlob, managedCred.CredentialBlob, 0, (int)unmanagedCred.CredentialBlobSize);
            }

            return(managedCred);
        }
Exemplo n.º 3
0
        /// <summary>
        /// マネージドな資格情報をアンマネージドなポインタに変換する
        /// </summary>
        /// <param name="managedCred"></param>
        /// <returns></returns>
        private static UnmanagedCredential ConvertToUnmanagedCredential(ManagedCredential managedCred)
        {
            var unmanagedCred = new UnmanagedCredential()
            {
                Flags = (uint)managedCred.Flags,
                Type = (uint)managedCred.Type,
                TargetName = managedCred.TargetName,
                Comment = managedCred.Comment,
                LastWritten = managedCred.LastWritten,
                CredentialBlobSize = managedCred.CredentialBlob != null ? (uint)managedCred.CredentialBlob.Length : 0,
                Persist = (uint)managedCred.Persist,
                TargetAlias = managedCred.TargetAlias,
                UserName = managedCred.UserName,
                AttributeCount = managedCred.Attributes != null ? (uint)managedCred.Attributes.Length : 0,
                Attributes = IntPtr.Zero,
                CredentialBlob = IntPtr.Zero,
            };


            if (unmanagedCred.AttributeCount != 0)
            {
                Marshal.Copy(managedCred.Attributes.Cast<int>().ToArray(), 0, unmanagedCred.Attributes, managedCred.Attributes.Length);
            }

            if (unmanagedCred.CredentialBlobSize != 0)
            {
                var p = Marshal.AllocHGlobal(managedCred.CredentialBlob.Length);
                Marshal.Copy(managedCred.CredentialBlob, 0, p, managedCred.CredentialBlob.Length);
                unmanagedCred.CredentialBlob = p;
                Marshal.FreeHGlobal(p);
            }

            return unmanagedCred;
        }
Exemplo n.º 4
0
        /// <summary>
        /// アンマネージドな資格情報(ポインタ)をマネージドなEnumに変換する
        /// </summary>
        /// <param name="ptr"></param>
        /// <returns></returns>
        private static ManagedCredential ConvertToManagedCredential(IntPtr ptr)
        {
            var unmanagedCred = (UnmanagedCredential)Marshal.PtrToStructure(ptr, typeof(UnmanagedCredential));


            // 特殊な操作が必要ないものを詰め込んだManagedCredentialを作成する
            // CredentialBlobは別途変換する必要があるため、一旦サイズだけを持った配列を作成する
            var managedCred = new ManagedCredential
            {
                Flags = (CRED_FLAGS)unmanagedCred.Flags,
                Type = (CRED_TYPE)unmanagedCred.Type,
                TargetName = unmanagedCred.TargetName,
                Comment = unmanagedCred.Comment,
                LastWritten = unmanagedCred.LastWritten,
                CredentialBlob = new byte[unmanagedCred.CredentialBlobSize],
                Persist = (CRED_PERSIST)unmanagedCred.Persist,
                Attributes = Enumerable.Range(0, (int)unmanagedCred.AttributeCount)
                                                .Select(x => Marshal.ReadIntPtr(unmanagedCred.Attributes, x * Marshal.SizeOf(typeof(CREDENTIAL_ATTRIBUTE))))
                                                .Select(x => (CREDENTIAL_ATTRIBUTE)Marshal.PtrToStructure(x, typeof(CREDENTIAL_ATTRIBUTE)))
                                                .ToArray(),
                TargetAlias = unmanagedCred.TargetAlias,
                UserName = unmanagedCred.UserName,
            };

            if (unmanagedCred.CredentialBlobSize != 0)
            {
                Marshal.Copy(unmanagedCred.CredentialBlob, managedCred.CredentialBlob, 0, (int)unmanagedCred.CredentialBlobSize);
            }

            return managedCred;
        }
Exemplo n.º 5
0
 /// <summary>
 /// 資格情報の削除
 /// </summary>
 /// <param name="cred">マネージドな資格情報</param>
 public static void Delete(ManagedCredential cred)
 {
     Delete(cred.TargetName, cred.Type, cred.Flags);
 }
Exemplo n.º 6
0
 /// <summary>
 /// 資格情報の登録
 /// </summary>
 /// <param name="managedCred"></param>
 /// <param name="flags"></param>
 public static void Write(ManagedCredential managedCred, CRED_FLAGS flags)
 {
     Write(ConvertToUnmanagedCredential(managedCred), flags);
 }
Exemplo n.º 7
0
 /// <summary>
 /// 資格情報の削除
 /// </summary>
 /// <param name="cred">マネージドな資格情報</param>
 public static void Delete(ManagedCredential cred)
 {
     Delete(cred.TargetName, cred.Type, cred.Flags);
 }
Exemplo n.º 8
0
 /// <summary>
 /// 資格情報の登録
 /// </summary>
 /// <param name="managedCred"></param>
 /// <param name="flags"></param>
 public static void Write(ManagedCredential managedCred, CRED_FLAGS flags)
 {
     Write(ConvertToUnmanagedCredential(managedCred), flags);
 }