Ejemplo n.º 1
0
        /// <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.
            return(NativeCode.CredWrite(ref ncred, 0));
        }
        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!");
            }
        }
Ejemplo n.º 3
0
 /// <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.
     try
     {
         return NativeCode.CredWrite(ref ncred, 0);
     }
     catch (Exception e)
     {
         log.error($"Failed saving credentials for {Target}", e);
         return false;
     }
 }
Ejemplo n.º 4
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               = Type;
     ncred.Persist            = (UInt32)Persist;
     ncred.UserName           = Marshal.StringToCoTaskMemUni(UserName);
     ncred.TargetName         = Marshal.StringToCoTaskMemUni(TargetName);
     ncred.CredentialBlob     = Marshal.StringToCoTaskMemUni(CredentialBlob);
     ncred.CredentialBlobSize = (UInt32)CredentialBlobSize;
     if (LastWritten != DateTime.MinValue)
     {
         var fileTime = LastWritten.ToFileTimeUtc();
         ncred.LastWritten.dwLowDateTime  = (int)(fileTime & 0xFFFFFFFFL);
         ncred.LastWritten.dwHighDateTime = (int)((fileTime >> 32) & 0xFFFFFFFFL);
     }
     return(ncred);
 }
Ejemplo n.º 5
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 (ArgumentOutOfRangeException e)
     {
     }
 }