CredUIPromptForCredentials from .NET with SecureString

本文关键字:SecureString with NET from CredUIPromptForCredentials | 更新日期: 2023-09-27 18:09:19

我想显示标准的系统对话框,要求用户提供帐户、用户名和密码,以便使用这些信息启动具有这些凭据的进程。

我已经指出了显示该对话框的CredUIPromptForCredentials函数。它以字符串形式返回用户名和密码。但是ProcessStartInfo结构期望的密码是SecureString

我明白我现在可以使用密码作为字符串并将其逐个字符转换为SecureString字符(没有单一的功能)-但它会完全击败SecureString背后的想法。

所以我猜一定有某种方法可以直接接受来自CredUIPromptForCredentials的非托管调用的密码作为。net中的SecureString。毕竟,我真的不需要以任何方式访问我的应用程序中的密码。它只是用来启动另一个进程,然后可以尽快忘记。

那么我的p/Invoke声明CredUIPromptForCredentials看起来像SecureString吗?(我从pinvoke.net的c#开始)

更新:哦,如果有人有一个例子,新的功能CredUIPromptForWindowsCredentials在Windows Vista/7,那将是很酷的,因为我甚至不知道如何使用它的时刻

CredUIPromptForCredentials from .NET with SecureString

可以将非托管字符串缓冲区的IntPtr强制转换为char*,并使用SecureString(char*, int)构造函数。

// somehow, we come into posession of an IntPtr to a string
// obviously, this would be a foolish way to come into it in
// production, since stringOriginalContents is already in managed
// code, and the lifetime can therefore not be guaranteed...
var stringOriginalContents = "foobar";
IntPtr strPtr = Marshal.StringToHGlobalUni(stringOriginalContents);
int strLen = stringOriginalContents.Length;
int maxLen = 100;
// we copy the IntPtr to a SecureString, and zero out the old location
SecureString ssNew;
unsafe
{
    char* strUPtr = (char*)strPtr;
    // if we don't know the length, calculate
    //for (strLen = 0; *(strUPtr + strLen) != ''0' 
    //    // stop if the string is invalid
    //    && strLen < maxLen; strLen++)
    //    ;
    ssNew = new SecureString((char*)strPtr, strLen);
    // zero out the old memory and release, or use a Zero Free method
    //for (int i = 0; i < strLen; i++)
    //    *(strUPtr + i) = ''0';
    //Marshal.FreeHGlobal(strPtr);
    // (only do one of these)
    Marshal.ZeroFreeGlobalAllocUnicode(strPtr);
}
// now the securestring has the protected data, and the old memory has been
// zeroed, we can check that the securestring is correct.  This, also should
// not be in production code.
string strInSecureString =
    Marshal.PtrToStringUni(
    Marshal.SecureStringToGlobalAllocUnicode(ssNew));
Assert.AreEqual(strInSecureString, stringOriginalContents);