everyone, I am facing an issue with LogonUser function.
I just want to know if I can import the LogonUser function into C# by this signature:
[DllImport("advapi32.dll", SetLastError = true)]
internal static extern int LogonUser(string username, string domain, IntPtr password, int logonType, int logonProvider, ref IntPtr token);
Because I want to secure my password not using a string, but a SecureString class. Then later the function is used like below:
var passwordPtr = Marshal.SecureStringToGlobalAllocUnicode(password);
var result = LogonUser(userName, domain, passwordPtr, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token);
I always get result = 0 and the message shows that the user name and password is incorrect. But when I change the signature to use string password, then everything works well.
Please help me as to secure the password with SecureString is important to me.
SecureStringToGlobalAllocUnicodehas a complete example for callingLogonUser: msdn.microsoft.com/en-us/library/…LogonUser- but I can't say for sure since you've not shown your P/Invoke signature.