@@ -16,7 +16,11 @@ namespace AdysTech.CredentialManager
1616 public static class CredentialManager
1717 {
1818
19-
19+ /// <summary>
20+ /// Opens OS Version specific Window prompting for credentials
21+ /// </summary>
22+ /// <param name="Target">A descriptive text for where teh credentials being asked are used for</param>
23+ /// <returns>NetworkCredential object containing the user name, </returns>
2024 public static NetworkCredential PromptForCredentials ( string Target )
2125 {
2226 var username = String . Empty ;
@@ -28,6 +32,13 @@ public static NetworkCredential PromptForCredentials(string Target)
2832 return new NetworkCredential ( username , passwd , domain ) ;
2933 }
3034
35+ /// <summary>
36+ /// Opens OS Version specific Window prompting for credentials
37+ /// </summary>
38+ /// <param name="Target">A descriptive text for where teh credentials being asked are used for</param>
39+ /// <param name="Message">A brief message to display in the dialog box</param>
40+ /// <param name="Caption">Title for the dialog box</param>
41+ /// <returns>NetworkCredential object containing the user name, </returns>
3142 public static NetworkCredential PromptForCredentials ( string Target , string Message , string Caption )
3243 {
3344 var username = String . Empty ;
@@ -96,7 +107,12 @@ private static bool PromptForCredentials(string target, NativeStructs.Credential
96107
97108
98109
99-
110+ /// <summary>
111+ /// Saves teh given Network Credential into Windows Credential store
112+ /// </summary>
113+ /// <param name="Target">Name of the application/Url where the credential is used for</param>
114+ /// <param name="credential">Credential to store</param>
115+ /// <returns>True:Success, False:Failure</returns>
100116 public static bool SaveCredentials ( string Target , NetworkCredential credential )
101117 {
102118 // Go ahead with what we have are stuff it into the CredMan structures.
@@ -118,6 +134,12 @@ public static bool SaveCredentials(string Target, NetworkCredential credential)
118134 }
119135 }
120136
137+
138+ /// <summary>
139+ /// Extract the stored credential from WIndows Credential store
140+ /// </summary>
141+ /// <param name="Target">Name of the application/Url where the credential is used for</param>
142+ /// <returns>null if target not found, else stored credentials</returns>
121143 public static NetworkCredential GetCredentials ( string Target )
122144 {
123145 IntPtr nCredPtr ;
@@ -141,7 +163,16 @@ public static NetworkCredential GetCredentials(string Target)
141163 var user = cred . UserName ;
142164 StringBuilder userBuilder = new StringBuilder ( ) ;
143165 StringBuilder domainBuilder = new StringBuilder ( ) ;
144- NativeStructs . CredUIParseUserName ( user , userBuilder , int . MaxValue , domainBuilder , int . MaxValue ) ;
166+ var ret1 = NativeStructs . CredUIParseUserName ( user , userBuilder , int . MaxValue , domainBuilder , int . MaxValue ) ;
167+ lastError = Marshal . GetLastWin32Error ( ) ;
168+
169+ //assuming invalid account name to be not meeting condition for CredUIParseUserName
170+ //"The name must be in UPN or down-level format, or a certificate"
171+ if ( ret1 == NativeStructs . CredentialUIReturnCodes . ERROR_INVALID_ACCOUNT_NAME )
172+ userBuilder . Append ( user ) ;
173+ else if ( ( uint ) ret1 > 0 )
174+ throw new Win32Exception ( lastError , "CredUIParseUserName throw an error" ) ;
175+
145176 username = userBuilder . ToString ( ) ;
146177 domain = domainBuilder . ToString ( ) ;
147178 return new NetworkCredential ( username , passwd , domain ) ;
@@ -150,6 +181,12 @@ public static NetworkCredential GetCredentials(string Target)
150181 return null ;
151182 }
152183
184+
185+ /// <summary>
186+ /// Remove stored credentials from windows credential store
187+ /// </summary>
188+ /// <param name="Target">Name of the application/Url where the credential is used for</param>
189+ /// <returns>True: Success, False: Failure</returns>
153190 public static bool RemoveCredentials ( string Target )
154191 {
155192 // Make the API call using the P/Invoke signature
@@ -160,6 +197,11 @@ public static bool RemoveCredentials(string Target)
160197 return ret ;
161198 }
162199
200+ /// <summary>
201+ /// Generates a string that can be used for "Auth" headers in web requests, "username:password" encoded in Base64
202+ /// </summary>
203+ /// <param name="cred"></param>
204+ /// <returns></returns>
163205 public static string GetBasicAuthString ( this NetworkCredential cred )
164206 {
165207 byte [ ] credentialBuffer = new UTF8Encoding ( ) . GetBytes ( cred . UserName + ":" + cred . Password ) ;
0 commit comments