private static bool ShowPrompt() { bool flag = false; int authError = 0; uint num = 0u; PromptForCredentials.CREDUI_INFO cREDUI_INFO = default(PromptForCredentials.CREDUI_INFO); cREDUI_INFO.cbSize = Marshal.SizeOf(cREDUI_INFO); cREDUI_INFO.pszCaptionText = "Connect to your application"; cREDUI_INFO.pszMessageText = "Enter your credentials!"; cREDUI_INFO.hwndParent = Application.MainWindow.Handle; while (true) { IntPtr intPtr; uint num3; uint num2 = PromptForCredentials.CredUIPromptForWindowsCredentials(ref cREDUI_INFO, authError, ref num, (IntPtr)0, 0u, out intPtr, out num3, ref flag, (PromptForCredentials.PromptForWindowsCredentialsFlags) 0); if (num2 != 0u) { break; } bool flag2 = false; if (flag2) { return(true); } authError = 1326; } return(false); }
public static bool GetCredentials(string captionText, string messageText, string userName, out NetworkCredential networkCredential) { if (PromptForCredentials.IsWinVistaOrHigher()) { return(PromptForCredentials.GetCredentialsNewStyle(captionText, messageText, userName, out networkCredential)); } return(PromptForCredentials.GetCredentialsOldSchool(captionText, messageText, userName, out networkCredential)); }
private static bool GetCredentialsNewStyle(string captionText, string messageText, string userName, out NetworkCredential networkCredential) { PromptForCredentials.CREDUI_INFO cREDUI_INFO = default(PromptForCredentials.CREDUI_INFO); cREDUI_INFO.pszCaptionText = captionText; cREDUI_INFO.pszMessageText = messageText; cREDUI_INFO.hwndParent = Application.MainWindow.Handle; string empty = string.Empty; int num = 0; IntPtr intPtr = IntPtr.Zero; PromptForCredentials.CredPackAuthenticationBuffer(0, userName, empty, IntPtr.Zero, ref num); if (num > 0) { intPtr = Marshal.AllocCoTaskMem(num); PromptForCredentials.CredPackAuthenticationBuffer(0, userName, empty, intPtr, ref num); } cREDUI_INFO.cbSize = Marshal.SizeOf(cREDUI_INFO); uint num2 = 0u; IntPtr intPtr2 = (IntPtr)0; bool flag = false; uint cbAuthBuffer; uint num3 = PromptForCredentials.CredUIPromptForWindowsCredentials(ref cREDUI_INFO, 0, ref num2, intPtr, 1024u, out intPtr2, out cbAuthBuffer, ref flag, PromptForCredentials.PromptForWindowsCredentialsFlags.CREDUIWIN_GENERIC); if (num3 != 0u) { networkCredential = null; return(false); } StringBuilder stringBuilder = new StringBuilder(100); StringBuilder stringBuilder2 = new StringBuilder(100); StringBuilder stringBuilder3 = new StringBuilder(100); int num4 = 100; int num5 = 100; int num6 = 100; if (num3 == 0u && PromptForCredentials.CredUnPackAuthenticationBuffer(0, intPtr2, cbAuthBuffer, stringBuilder, ref num4, stringBuilder3, ref num5, stringBuilder2, ref num6)) { PromptForCredentials.CoTaskMemFree(intPtr2); networkCredential = new NetworkCredential { UserName = stringBuilder.ToString(), Password = stringBuilder2.ToString(), Domain = stringBuilder3.ToString() }; return(true); } networkCredential = null; return(false); }
private static bool GetCredentialsOldSchool(string captionText, string messageText, string userName, out NetworkCredential networkCredential) { StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder2 = new StringBuilder(userName); PromptForCredentials.CREDUI_INFO cREDUI_INFO = default(PromptForCredentials.CREDUI_INFO); cREDUI_INFO.cbSize = Marshal.SizeOf(cREDUI_INFO); bool flag = false; cREDUI_INFO.pszCaptionText = captionText; PromptForCredentials.CREDUI_FLAGS flags = PromptForCredentials.CREDUI_FLAGS.ALWAYS_SHOW_UI | PromptForCredentials.CREDUI_FLAGS.GENERIC_CREDENTIALS; if (PromptForCredentials.CredUIPromptForCredentials(ref cREDUI_INFO, messageText, IntPtr.Zero, 0, stringBuilder2, 100, stringBuilder, 100, ref flag, flags) == PromptForCredentials.CredUIReturnCodes.NO_ERROR) { string userName2 = stringBuilder2.ToString(); string password = stringBuilder.ToString(); networkCredential = new NetworkCredential(userName2, password); return(true); } networkCredential = null; return(false); }