private void ManageCredentialsStorage(IntPtr outBuffer, uint outBufferSize, bool storedCredentials)
{
var userName = new StringBuilder(CredUi.CREDUI_MAX_USERNAME_LENGTH);
var password = new StringBuilder(CredUi.CREDUI_MAX_PASSWORD_LENGTH);
var userNameSize = (uint)userName.Capacity;
var passwordSize = (uint)password.Capacity;
uint domainSize = 0;
if (!CredUi.CredUnPackAuthenticationBuffer(0, outBuffer, outBufferSize, userName, ref userNameSize, null, ref domainSize, password, ref passwordSize))
{
throw Log.ErrorAndCreateException(x => new CredentialException(Marshal.GetLastWin32Error()),
"Failed to create the authentication buffer after prompting");
}
UserName = userName.ToString();
Password = password.ToString();
Log.Debug("User entered credentials with username '{0}'", UserName);
if (!ShowSaveCheckBox)
{
return;
}
// If the NativeCredential was stored previously but the user has now cleared the save checkbox,
// we want to delete the NativeCredential.
if (storedCredentials && !IsSaveChecked)
{
DeleteCredential(Target);
}
if (IsSaveChecked)
{
WriteCredential(Target, UserName, Password);
}
}