gpt4 book ai didi

delphi - 使用 Windows 凭据管理器时切换用户名和密码

转载 作者:行者123 更新时间:2023-12-02 03:44:12 24 4
gpt4 key购买 nike

我从 SO 得到了这个 Delphi 代码,看起来非常简单。我添加了一些缺少的类型并替换了一些缺少的字符串处理函数,并得到了这个。

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;

密码显示在用户名中。

enter image description here

这让我很困惑。 .Username 字段不应该采用用户名吗?并且密码写入“credentialBlob”?我已经检查了所有函数签名,以确保我没有错误地切换内容。

最佳答案

假设您使用的是 DUnitX,TestCase 属性的第一个参数是测试用例名称,后面是参数。因此,您定义了一个名为“user”的测试用例,将“pass”作为用户名传递,不传递任何内容作为密码。试试这个:

[TestCase('TestInsertCred', 'user,pass')]

关于delphi - 使用 Windows 凭据管理器时切换用户名和密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48568435/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com