Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit WinCred;
- interface
- uses
- Windows, SysUtils;
- type
- CREDENTIAL_ATTRIBUTE = packed record
- Keyword: LPTSTR;
- Flags: DWORD;
- ValueSize: DWORD;
- Value: LPBYTE;
- end;
- PCREDENTIAL_ATTRIBUTE = ^CREDENTIAL_ATTRIBUTE;
- CREDENTIALW = packed record
- Flags: DWORD;
- Type_: DWORD;
- TargetName: LPTSTR;
- Comment: LPTSTR;
- LastWritten: FILETIME;
- CredentialBlobSize: DWORD;
- CredentialBlob: LPBYTE;
- Persist: DWORD;
- AttributeCount: DWORD;
- Attributes: PCREDENTIAL_ATTRIBUTE;
- TargetAlias: LPTSTR;
- UserName: LPTSTR;
- end;
- PCREDENTIALW = ^CREDENTIALW;
- function CredWriteW(Credential: PCREDENTIALW; Flags: DWORD): Boolean; stdcall; external 'Advapi32.dll';
- function CredWriteGenericCredentials(const Target, Username, Password: UnicodeString): Boolean;
- implementation
- function CredWriteGenericCredentials(const Target, Username, Password: UnicodeString): Boolean;
- var
- Credentials: CREDENTIALW;
- begin
- ZeroMemory(@Credentials, SizeOf(Credentials));
- Credentials.TargetName := PWideChar(Target); //cannot be longer than CRED_MAX_GENERIC_TARGET_NAME_LENGTH (32767) characters. Recommended format "Company_Target"
- Credentials.Type_ := CRED_TYPE_GENERIC;
- Credentials.UserName := PWideChar(Username);
- Credentials.Persist := CRED_PERSIST_LOCAL_MACHINE;
- Credentials.CredentialBlob := PByte(Password);
- Credentials.CredentialBlobSize := 2*(Length(Password)); //By convention no trailing null. Cannot be longer than CRED_MAX_CREDENTIAL_BLOB_SIZE (512) bytes
- Result := CredWriteW(@Credentials, 0);
- end;
- end.
- [Test]
- [TestCase('user', 'pass')]
- procedure TestInsertCred(Username, password: string);
- procedure TWinCredTests.TestInsertCred(Username, password: string);
- begin
- Assert.IsTrue(CredWriteGenericCredentials('WinCred_Test', Username, password));
- end;
Add Comment
Please, Sign In to add comment