Skip to content

Commit 098f5ab

Browse files
committed
Bug fixes, comments
1 parent 1959498 commit 098f5ab

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

AdysTech.CredentialManager/CredentialManager.cs

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

AdysTech.CredentialManager/NativeStructs.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ internal enum CredentialUIFlags
3131
KEEP_USERNAME = 0x100000,
3232
}
3333

34-
internal enum CredentialUIReturnCodes
34+
internal enum CredentialUIReturnCodes : uint
3535
{
3636
NO_ERROR = 0,
3737
ERROR_CANCELLED = 1223,
@@ -65,7 +65,7 @@ internal static extern CredentialUIReturnCodes CredUIPromptForCredentials(ref Cr
6565
[MarshalAs (UnmanagedType.Bool)] ref bool pfSave,
6666
CredentialUIFlags flags);
6767

68-
[DllImport ("credui.dll", EntryPoint = "CredUIParseUserNameW", CharSet = CharSet.Unicode)]
68+
[DllImport ("credui.dll", EntryPoint = "CredUIParseUserNameW", CharSet = CharSet.Unicode, SetLastError = true)]
6969
internal static extern CredentialUIReturnCodes CredUIParseUserName(
7070
string userName,
7171
StringBuilder user,
@@ -102,7 +102,7 @@ internal struct NativeCredential
102102
public IntPtr Attributes;
103103
public IntPtr TargetAlias;
104104
public IntPtr UserName;
105-
105+
106106
}
107107

108108
[DllImport ("Advapi32.dll", EntryPoint = "CredDeleteW", CharSet = CharSet.Unicode, SetLastError = true)]

CredentialManager.Test/CredentialManagerTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void TestGetCredentials()
3131

3232
try
3333
{
34-
Assert.IsNotNull (CredentialManager.GetCredentials ("TestSystem"), "GetCredential failed");
34+
Assert.IsNotNull (CredentialManager.GetCredentials ("localhost:8086"), "GetCredential failed");
3535
}
3636
catch ( Exception e )
3737
{

0 commit comments

Comments
 (0)