@@ -13,8 +13,6 @@ public class WindowsCredentialManager : ICredentialStore
13
13
14
14
private readonly string _namespace ;
15
15
16
- #region Constructors
17
-
18
16
/// <summary>
19
17
/// Open the Windows Credential Manager vault for the current user.
20
18
/// </summary>
@@ -26,10 +24,6 @@ public WindowsCredentialManager(string @namespace = null)
26
24
_namespace = @namespace ;
27
25
}
28
26
29
- #endregion
30
-
31
- #region ICredentialStore
32
-
33
27
public ICredential Get ( string service , string account )
34
28
{
35
29
return Enumerate ( service , account ) . FirstOrDefault ( ) ;
@@ -66,6 +60,15 @@ public void AddOrUpdate(string service, string account, string secret)
66
60
// Create new entry with the account in the target name
67
61
targetName = CreateTargetName ( service , account ) ;
68
62
}
63
+ else
64
+ {
65
+ // No need to write out credential if the account and secret/password are the same
66
+ string existingSecret = existingCred . GetCredentialBlobAsString ( ) ;
67
+ if ( StringComparer . Ordinal . Equals ( existingSecret , secret ) )
68
+ {
69
+ return ;
70
+ }
71
+ }
69
72
}
70
73
71
74
byte [ ] secretBytes = Encoding . Unicode . GetBytes ( secret ) ;
@@ -129,8 +132,6 @@ public bool Remove(string service, string account)
129
132
return false ;
130
133
}
131
134
132
- #endregion
133
-
134
135
/// <summary>
135
136
/// Check if we can persist credentials to for the current process and logon session.
136
137
/// </summary>
@@ -205,14 +206,7 @@ private IEnumerable<WindowsCredential> Enumerate(string service, string account)
205
206
206
207
private WindowsCredential CreateCredentialFromStructure ( Win32Credential credential )
207
208
{
208
- string password = null ;
209
- if ( credential . CredentialBlobSize != 0 && credential . CredentialBlob != IntPtr . Zero )
210
- {
211
- byte [ ] passwordBytes = InteropUtils . ToByteArray (
212
- credential . CredentialBlob ,
213
- credential . CredentialBlobSize ) ;
214
- password = Encoding . Unicode . GetString ( passwordBytes ) ;
215
- }
209
+ string password = credential . GetCredentialBlobAsString ( ) ;
216
210
217
211
// Recover the target name we gave from the internal (raw) target name
218
212
string targetName = credential . TargetName . TrimUntilIndexOf ( TargetNameLegacyGenericPrefix ) ;
0 commit comments