Guest User

Untitled

a guest
Feb 1st, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. unit WinCred;
  2.  
  3. interface
  4.  
  5. uses
  6. Windows, SysUtils;
  7.  
  8. type
  9. CREDENTIAL_ATTRIBUTE = packed record
  10. Keyword: LPTSTR;
  11. Flags: DWORD;
  12. ValueSize: DWORD;
  13. Value: LPBYTE;
  14. end;
  15. PCREDENTIAL_ATTRIBUTE = ^CREDENTIAL_ATTRIBUTE;
  16.  
  17. CREDENTIALW = packed record
  18. Flags: DWORD;
  19. Type_: DWORD;
  20. TargetName: LPTSTR;
  21. Comment: LPTSTR;
  22. LastWritten: FILETIME;
  23. CredentialBlobSize: DWORD;
  24. CredentialBlob: LPBYTE;
  25. Persist: DWORD;
  26. AttributeCount: DWORD;
  27. Attributes: PCREDENTIAL_ATTRIBUTE;
  28. TargetAlias: LPTSTR;
  29. UserName: LPTSTR;
  30. end;
  31. PCREDENTIALW = ^CREDENTIALW;
  32.  
  33. function CredWriteW(Credential: PCREDENTIALW; Flags: DWORD): Boolean; stdcall; external 'Advapi32.dll';
  34.  
  35. function CredWriteGenericCredentials(const Target, Username, Password: UnicodeString): Boolean;
  36.  
  37. implementation
  38.  
  39. function CredWriteGenericCredentials(const Target, Username, Password: UnicodeString): Boolean;
  40. var
  41. Credentials: CREDENTIALW;
  42. begin
  43. ZeroMemory(@Credentials, SizeOf(Credentials));
  44. Credentials.TargetName := PWideChar(Target); //cannot be longer than CRED_MAX_GENERIC_TARGET_NAME_LENGTH (32767) characters. Recommended format "Company_Target"
  45. Credentials.Type_ := CRED_TYPE_GENERIC;
  46. Credentials.UserName := PWideChar(Username);
  47. Credentials.Persist := CRED_PERSIST_LOCAL_MACHINE;
  48. Credentials.CredentialBlob := PByte(Password);
  49. Credentials.CredentialBlobSize := 2*(Length(Password)); //By convention no trailing null. Cannot be longer than CRED_MAX_CREDENTIAL_BLOB_SIZE (512) bytes
  50. Result := CredWriteW(@Credentials, 0);
  51. end;
  52.  
  53. end.
  54.  
  55. [Test]
  56. [TestCase('user', 'pass')]
  57. procedure TestInsertCred(Username, password: string);
  58.  
  59. procedure TWinCredTests.TestInsertCred(Username, password: string);
  60. begin
  61. Assert.IsTrue(CredWriteGenericCredentials('WinCred_Test', Username, password));
  62. end;
Add Comment
Please, Sign In to add comment