认为我成功地为这里提到的 Credential API 函数创建了一个托管包装类,并得到了一些帮助至少从该函数返回的 Win32-Errorcodes 是零或其他但预期的(即,如果调用两次,则来自 CredDelete 的1168 )和适当的值存储在注册表中的正确位置 (HKLM/Comm/Security/Credman/1..)。
现在,我正在使用嵌入在 PPC 上的 WindowsForm 中的 Webbrowser-Control 对使用 NTLM-Authentication 的网站进行身份验证。我不想让弹出对话框出现,用户必须在其中输入他的凭据。相反,我让用户可以将他的凭据存储在他首先在 Optiondialog-Form 中输入的设备上(实习生调用 CredWrite/CredUpdate)。
但是 PIE 对我使用 API 所做的事情感到非常厌恶,CredWrite、-Update 或 -Delete 都没有真正起作用。那么我在这里错过了什么?
CredWrite 的示例代码:
[DllImport("coredll.dll", CharSet = CharSet.Unicode, SetLastError = true)]
static extern int CredWrite([In]IntPtr pCred, [In]CREDWRITE_FLAGS dwflags);
public enum CREDWRITE_FLAGS : int
{
CRED_FLAG_FAIL_IF_EXISTING = 0x00000400
}
public struct CRED
{
public int dwVersion;
public CRED_TYPE dwType;
[MarshalAs(UnmanagedType.LPWStr)]
public string wszUser;
public int dwUserLen;
[MarshalAs(UnmanagedType.LPWStr)]
public string wszTarget;
public int dwTargetLen;
public IntPtr pBlob;
public int dwBlobSize;
public CRED_FLAGS dwFlags;
}
public enum CRED_TYPE
{
CRED_TYPE_NTLM = 0x00010002,
CRED_TYPE_KERBEROS = 0x00010004,
CRED_TYPE_PLAINTEXT_PASSWORD = 0x00010006,
CRED_TYPE_CERTIFICATE = 0x00010008,
CRED_TYPE_GENERIC = 0x0001000a,
CRED_TYPE_DOMAIN_PASSWORD = 0x00010001,
}
public enum CRED_FLAGS : int
{
CRED_FLAG_PERSIST = 0x00000001,
CRED_FLAG_DEFAULT = 0x00000002,
CRED_FLAG_SENSITIVE = 0x00000008,
CRED_FLAG_TRUSTED = 0x00000010
}
public static void WriteCredentials(string target, string userName, string password)
{
CRED cred = new CRED();
cred.dwVersion = 1;
cred.dwType = CRED_TYPE.CRED_TYPE_NTLM;
cred.wszTarget = target;
cred.dwTargetLen = target.Length + 1;
cred.wszUser = userName;
cred.dwUserLen = userName.Length + 1;
cred.dwBlobSize = (Encoding.Unicode.GetBytes(password).Length + 1) * 2;
//cred.pBlob = Marshal.StringToCoTaskMemUni(password); //<--not in CF
//cred.pBlob = Marshal2.StringToHGlobalUni(password); //<--from OpenNETCF, the same?
cred.pBlob = Marshal.StringToBSTR(password); //<--not sure of that, but tried the other one also
cred.dwFlags = CRED_FLAGS.CRED_FLAG_PERSIST | CRED_FLAGS.CRED_FLAG_SENSITIVE | CRED_FLAGS.CRED_FLAG_TRUSTED; //<-- results in 25 which is also used in creds read which are stored by the IE-UI-CredMan-dialog
IntPtr credPtr = Marshal.AllocHGlobal(Marshal.SizeOf(cred));
Marshal.StructureToPtr(cred, credPtr, true);
int ret = -1;
ret = CredWrite(credPtr, CREDWRITE_FLAGS.CRED_FLAG_FAIL_IF_EXISTING); //returns zero, unless called twice with the same target/username-tuple.
Marshal.FreeHGlobal(credPtr);
}
顺便说一句,所谓的“MS-Experts”提到 PIE 有自己的凭据缓存机制,这就是为什么 PIE 忽略 CredUpdate 上的更改。但我怀疑这是 100% 正确的,因为当我在根本没有凭据的设备上调用 CredWrite 时,PIE 也会忽略它们(弹出窗口 cred-inputdialog)。
有人可以帮助我吗?