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!"); } }
/// <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); } }