internal Credential GetCredential()
        {
            if (!IsInvalid)
            {
                // Get the Credential from the mem location
                NativeCode.NativeCredential ncred = (NativeCode.NativeCredential)Marshal.PtrToStructure(handle,
                                                                                                        typeof(NativeCode.NativeCredential));

                // Create a managed Credential type and fill it with data from the native counterpart.
                Credential cred = new Credential(ncred);

                return(cred);
            }
            else
            {
                throw new InvalidOperationException("Invalid CriticalHandle!");
            }
        }
Esempio n. 2
0
 internal Credential(NativeCode.NativeCredential ncred)
 {
     CredentialBlobSize = ncred.CredentialBlobSize;
     CredentialBlob     = Marshal.PtrToStringUni(ncred.CredentialBlob,
                                                 (int)ncred.CredentialBlobSize / 2);
     UserName    = Marshal.PtrToStringUni(ncred.UserName);
     TargetName  = Marshal.PtrToStringUni(ncred.TargetName);
     TargetAlias = Marshal.PtrToStringUni(ncred.TargetAlias);
     Type        = ncred.Type;
     Flags       = ncred.Flags;
     Persist     = (NativeCode.Persistance)ncred.Persist;
     try
     {
         LastWritten = DateTime.FromFileTime((long)((ulong)ncred.LastWritten.dwHighDateTime << 32 | (ulong)ncred.LastWritten.dwLowDateTime));
     }
     catch
     {
     }
 }
Esempio n. 3
0
 /// <summary>
 /// This method derives a NativeCredential instance from a given Credential instance.
 /// </summary>
 /// <param name="cred">The managed Credential counterpart containing data to be stored.</param>
 /// <returns>A NativeCredential instance that is derived from the given Credential
 /// instance.</returns>
 internal NativeCode.NativeCredential GetNativeCredential()
 {
     NativeCode.NativeCredential ncred = new NativeCode.NativeCredential();
     ncred.AttributeCount     = 0;
     ncred.Attributes         = IntPtr.Zero;
     ncred.Comment            = IntPtr.Zero;
     ncred.TargetAlias        = IntPtr.Zero;
     ncred.Type               = this.Type;
     ncred.Persist            = (UInt32)this.Persist;
     ncred.UserName           = Marshal.StringToCoTaskMemUni(this.UserName);
     ncred.TargetName         = Marshal.StringToCoTaskMemUni(this.TargetName);
     ncred.CredentialBlob     = Marshal.StringToCoTaskMemUni(this.CredentialBlob);
     ncred.CredentialBlobSize = (UInt32)this.CredentialBlobSize;
     if (this.LastWritten != DateTime.MinValue)
     {
         var fileTime = this.LastWritten.ToFileTimeUtc();
         ncred.LastWritten.dwLowDateTime  = (int)(fileTime & 0xFFFFFFFFL);
         ncred.LastWritten.dwHighDateTime = (int)((fileTime >> 32) & 0xFFFFFFFFL);
     }
     return(ncred);
 }
        /// <summary>
        /// Saves teh given Network Credential into Windows Credential store
        /// </summary>
        /// <param name="Target">Name of the application/Url where the credential is used for</param>
        /// <param name="credential">Credential to store</param>
        /// <returns>True:Success, False:Failure</returns>
        public static bool SaveCredentials(string Target, NetworkCredential credential)
        {
            // Go ahead with what we have are stuff it into the CredMan structures.
            Credential cred = new Credential(credential);

            cred.TargetName = Target;
            cred.Persist    = NativeCode.Persistance.Entrprise;
            NativeCode.NativeCredential ncred = cred.GetNativeCredential();
            // Write the info into the CredMan storage.
            bool written   = NativeCode.CredWrite(ref ncred, 0);
            int  lastError = Marshal.GetLastWin32Error();

            if (written)
            {
                return(true);
            }
            else
            {
                string message = string.Format("CredWrite failed with the error code {0}.", lastError);
                throw new Exception(message);
            }
        }