|
| 1 | +using Microsoft.Win32.SafeHandles; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.ComponentModel; |
| 5 | +using System.Linq; |
| 6 | +using System.Net; |
| 7 | +using System.Runtime.InteropServices; |
| 8 | +using System.Text; |
| 9 | +using System.Threading.Tasks; |
| 10 | + |
| 11 | +namespace AdysTech.CredentialManager |
| 12 | +{ |
| 13 | + |
| 14 | + //ref: http://blogs.msdn.com/b/peerchan/archive/2005/11/01/487834.aspx |
| 15 | + |
| 16 | + public static class CredentialManager |
| 17 | + { |
| 18 | + |
| 19 | + |
| 20 | + public static NetworkCredential PromptForCredentials(string Target) |
| 21 | + { |
| 22 | + var username = String.Empty; |
| 23 | + var passwd = String.Empty; |
| 24 | + var domain = String.Empty; |
| 25 | + |
| 26 | + if ( !PromptForCredentials (Target, out username, out passwd, out domain) ) |
| 27 | + return null; |
| 28 | + return new NetworkCredential (username, passwd, domain); |
| 29 | + } |
| 30 | + |
| 31 | + public static NetworkCredential PromptForCredentials(string Target, string Message, string Caption) |
| 32 | + { |
| 33 | + var username = String.Empty; |
| 34 | + var passwd = String.Empty; |
| 35 | + var domain = String.Empty; |
| 36 | + |
| 37 | + if ( !PromptForCredentials (Target, Message, Caption, out username, out passwd, out domain) ) |
| 38 | + return null; |
| 39 | + return new NetworkCredential (username, passwd, domain); |
| 40 | + } |
| 41 | + |
| 42 | + internal static bool PromptForCredentials(string target, out string user, out string password, out string domain) |
| 43 | + { |
| 44 | + return PromptForCredentials (target, new NativeStructs.CredentialUIInfo (), out user, out password, out domain); |
| 45 | + } |
| 46 | + |
| 47 | + internal static bool PromptForCredentials(string target, string Message, string Caption, out string user, out string password, out string domain) |
| 48 | + { |
| 49 | + NativeStructs.CredentialUIInfo credUI = new NativeStructs.CredentialUIInfo (); |
| 50 | + credUI.pszMessageText = Message; |
| 51 | + credUI.pszCaptionText = Caption; |
| 52 | + return PromptForCredentials (target, credUI, out user, out password, out domain); |
| 53 | + } |
| 54 | + |
| 55 | + private static bool PromptForCredentials(string target, NativeStructs.CredentialUIInfo credUI, out string user, out string password, out string domain) |
| 56 | + { |
| 57 | + // Setup the flags and variables |
| 58 | + StringBuilder userPassword = new StringBuilder (), userID = new StringBuilder (); |
| 59 | + credUI.cbSize = Marshal.SizeOf (credUI); |
| 60 | + bool save = true; |
| 61 | + NativeStructs.CredentialUIFlags flags = NativeStructs.CredentialUIFlags.COMPLETE_USERNAME | NativeStructs.CredentialUIFlags.PERSIST | NativeStructs.CredentialUIFlags.EXCLUDE_CERTIFICATES; |
| 62 | + |
| 63 | + // Prompt the user |
| 64 | + NativeStructs.CredentialUIReturnCodes returnCode = NativeStructs.CredUIPromptForCredentials (ref credUI, target, IntPtr.Zero, 0, userID, 100, userPassword, 100, ref save, flags); |
| 65 | + |
| 66 | + password = userPassword.ToString (); |
| 67 | + |
| 68 | + StringBuilder userBuilder = new StringBuilder (); |
| 69 | + StringBuilder domainBuilder = new StringBuilder (); |
| 70 | + |
| 71 | + returnCode = NativeStructs.CredUIParseUserName (userID.ToString (), userBuilder, int.MaxValue, domainBuilder, int.MaxValue); |
| 72 | + switch ( returnCode ) |
| 73 | + { |
| 74 | + case NativeStructs.CredentialUIReturnCodes.NO_ERROR: // The username is valid. |
| 75 | + user = userBuilder.ToString (); |
| 76 | + domain = domainBuilder.ToString (); |
| 77 | + return true; |
| 78 | + |
| 79 | + case NativeStructs.CredentialUIReturnCodes.ERROR_INVALID_ACCOUNT_NAME: // The username is not valid. |
| 80 | + user = userID.ToString (); |
| 81 | + domain = null; |
| 82 | + return false; |
| 83 | + |
| 84 | + case NativeStructs.CredentialUIReturnCodes.ERROR_INSUFFICIENT_BUFFER: // One of the buffers is too small. |
| 85 | + throw new OutOfMemoryException (); |
| 86 | + |
| 87 | + case NativeStructs.CredentialUIReturnCodes.ERROR_INVALID_PARAMETER: // ulUserMaxChars or ulDomainMaxChars is zero OR userName, user, or domain is NULL. |
| 88 | + throw new ArgumentNullException ("userName"); |
| 89 | + |
| 90 | + default: |
| 91 | + user = null; |
| 92 | + domain = null; |
| 93 | + return false; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + public static bool SaveCredentials(string Target, NetworkCredential credential) |
| 101 | + { |
| 102 | + // Go ahead with what we have are stuff it into the CredMan structures. |
| 103 | + Credential cred = new Credential (credential); |
| 104 | + cred.TargetName = Target; |
| 105 | + cred.Persist = NativeStructs.Persistance.ENTERPRISE; |
| 106 | + NativeStructs.NativeCredential ncred = cred.GetNativeCredential (); |
| 107 | + // Write the info into the CredMan storage. |
| 108 | + bool written = NativeStructs.CredWrite (ref ncred, 0); |
| 109 | + int lastError = Marshal.GetLastWin32Error (); |
| 110 | + if ( written ) |
| 111 | + { |
| 112 | + return true; |
| 113 | + } |
| 114 | + else |
| 115 | + { |
| 116 | + string message = string.Format ("CredWrite failed with the error code {0}.", lastError); |
| 117 | + throw new Exception (message); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + public static NetworkCredential GetCredentials(string Target) |
| 122 | + { |
| 123 | + IntPtr nCredPtr; |
| 124 | + var username = String.Empty; |
| 125 | + var passwd = String.Empty; |
| 126 | + var domain = String.Empty; |
| 127 | + |
| 128 | + // Make the API call using the P/Invoke signature |
| 129 | + bool ret = NativeStructs.CredRead (Target, NativeStructs.CredentialType.GENERIC, 0, out nCredPtr); |
| 130 | + int lastError = Marshal.GetLastWin32Error (); |
| 131 | + if ( !ret ) |
| 132 | + throw new Win32Exception (lastError, "CredDelete throw an error"); |
| 133 | + |
| 134 | + // If the API was successful then... |
| 135 | + if ( ret ) |
| 136 | + { |
| 137 | + using ( CriticalCredentialHandle critCred = new CriticalCredentialHandle (nCredPtr) ) |
| 138 | + { |
| 139 | + Credential cred = critCred.GetCredential (); |
| 140 | + passwd = cred.CredentialBlob; |
| 141 | + var user = cred.UserName; |
| 142 | + StringBuilder userBuilder = new StringBuilder (); |
| 143 | + StringBuilder domainBuilder = new StringBuilder (); |
| 144 | + NativeStructs.CredUIParseUserName (user, userBuilder, int.MaxValue, domainBuilder, int.MaxValue); |
| 145 | + username = userBuilder.ToString (); |
| 146 | + domain = domainBuilder.ToString (); |
| 147 | + return new NetworkCredential (username, passwd, domain); |
| 148 | + } |
| 149 | + } |
| 150 | + return null; |
| 151 | + } |
| 152 | + |
| 153 | + public static bool RemoveCredentials(string Target) |
| 154 | + { |
| 155 | + // Make the API call using the P/Invoke signature |
| 156 | + var ret = NativeStructs.CredDelete (Target, NativeStructs.CredentialType.GENERIC, 0); |
| 157 | + int lastError = Marshal.GetLastWin32Error (); |
| 158 | + if ( !ret ) |
| 159 | + throw new Win32Exception (lastError, "CredDelete throw an error"); |
| 160 | + return ret; |
| 161 | + } |
| 162 | + |
| 163 | + public static string GetBasicAuthString(this NetworkCredential cred) |
| 164 | + { |
| 165 | + byte[] credentialBuffer = new UTF8Encoding ().GetBytes (cred.UserName + ":" + cred.Password); |
| 166 | + return Convert.ToBase64String (credentialBuffer); |
| 167 | + } |
| 168 | + } |
| 169 | +} |
| 170 | + |
0 commit comments