private static SafeNativeMemory PackAuthenticationBuffer(NetworkCredential credential) { var userName = credential.Maybe().Return(c => c.UserName); if (string.IsNullOrEmpty(userName)) return new SafeNativeMemory(); var inCredSize = 0; NativeMethods.CredPackAuthenticationBuffer(0, userName, string.Empty, IntPtr.Zero, ref inCredSize); var buffer = new SafeNativeMemory(inCredSize); NativeMethods.CredPackAuthenticationBuffer(0, userName, string.Empty, buffer.DangerousGetHandle(), ref inCredSize); return buffer; }