tankcr

login

Jul 21st, 2014
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.31 KB | None | 0 0
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Security;
  8.  
  9.  
  10. namespace SharePoint_Info
  11. {
  12.    
  13.     public class creds
  14.     {
  15.         private class UNPW
  16.         {
  17.             private List<UnPw> loginCreds;
  18.             public List<UnPw> LoginCreds
  19.             {
  20.                 get { return loginCreds; }
  21.                 set
  22.                 {
  23.                     loginCreds = value;
  24.                 }
  25.             }
  26.         }
  27.     }
  28.     class creduiprompt
  29.     {
  30.         /// <summary>
  31.         /// Leverages the windows UI to prompt for a password
  32.         /// </summary>
  33.         internal static class Authentication
  34.         {
  35.             public struct CREDUI_INFO
  36.             {
  37.                 public int cbSize;
  38.                 public IntPtr hwndParent;
  39.                 public string pszMessageText;
  40.                 public string pszCaptionText;
  41.                 public IntPtr hbmBanner;
  42.             }
  43.  
  44.             [DllImport("credui")]
  45.             private static extern CredUIReturnCodes CredUIPromptForCredentials(ref CREDUI_INFO creditUR,
  46.                   //string targetName,
  47.                   IntPtr reserved1,
  48.                   int iError,
  49.                   StringBuilder userName,
  50.                   int maxUserName,
  51.                   StringBuilder password,
  52.                   int maxPassword,
  53.                   [MarshalAs(UnmanagedType.Bool)] ref bool pfSave,
  54.                   CREDUI_FLAGS flags);
  55.  
  56.             [Flags]
  57.             enum CREDUI_FLAGS
  58.             {
  59.                 INCORRECT_PASSWORD = 0x1,
  60.                 DO_NOT_PERSIST = 0x2,
  61.                 REQUEST_ADMINISTRATOR = 0x4,
  62.                 EXCLUDE_CERTIFICATES = 0x8,
  63.                 REQUIRE_CERTIFICATE = 0x10,
  64.                 SHOW_SAVE_CHECK_BOX = 0x40,
  65.                 ALWAYS_SHOW_UI = 0x80,
  66.                 REQUIRE_SMARTCARD = 0x100,
  67.                 PASSWORD_ONLY_OK = 0x200,
  68.                 VALIDATE_USERNAME = 0x400,
  69.                 COMPLETE_USERNAME = 0x800,
  70.                 PERSIST = 0x1000,
  71.                 SERVER_CREDENTIAL = 0x4000,
  72.                 EXPECT_CONFIRMATION = 0x20000,
  73.                 GENERIC_CREDENTIALS = 0x40000,
  74.                 USERNAME_TARGET_CREDENTIALS = 0x80000,
  75.                 KEEP_USERNAME = 0x100000,
  76.             }
  77.  
  78.             public enum CredUIReturnCodes
  79.             {
  80.                 NO_ERROR = 0,
  81.                 ERROR_CANCELLED = 1223,
  82.                 ERROR_NO_SUCH_LOGON_SESSION = 1312,
  83.                 ERROR_NOT_FOUND = 1168,
  84.                 ERROR_INVALID_ACCOUNT_NAME = 1315,
  85.                 ERROR_INSUFFICIENT_BUFFER = 122,
  86.                 ERROR_INVALID_PARAMETER = 87,
  87.                 ERROR_INVALID_FLAGS = 1004,
  88.             }
  89.  
  90.             /// <summary>
  91.             /// Prompts for password.
  92.             /// </summary>
  93.             /// <param name="user">The user.</param>
  94.             /// <param name="password">The password.</param>
  95.             /// <returns>True if no errors.</returns>
  96.             internal static List<UnPw> PromptForPassword(out string user, out string password)
  97.             {
  98.                 // Setup the flags and variables
  99.                 StringBuilder userPassword = new StringBuilder(), userID = new StringBuilder();
  100.                 CREDUI_INFO credUI = new CREDUI_INFO();
  101.                 credUI.cbSize = Marshal.SizeOf(credUI);
  102.                 bool save = false;
  103.                 CREDUI_FLAGS flags = CREDUI_FLAGS.ALWAYS_SHOW_UI | CREDUI_FLAGS.GENERIC_CREDENTIALS;
  104.  
  105.                 // Prompt the user
  106.                 CredUIReturnCodes returnCode = CredUIPromptForCredentials(ref credUI, IntPtr.Zero, 0, userID, 100, userPassword, 100, ref save, flags);
  107.  
  108.                 user = userID.ToString();
  109.                 password = userPassword.ToString();
  110.                 var usersec = new SecureString();
  111.                 foreach (char c in password)
  112.                 {
  113.                     usersec.AppendChar(c);
  114.                 }
  115.                 List<UnPw> cred = new List<UnPw>();
  116.                 UnPw item = new UnPw();
  117.                 {
  118.                     item.UserName = user;
  119.                     item.UserPW = usersec;
  120.                     cred.Add(item);
  121.                 }
  122.                 return cred;
  123.             }
  124.         }
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment