Esempio n. 1
0
        /// <summary>
        /// Extract the stored credential from WIndows Credential store
        /// </summary>
        /// <param name="Target">Name of the application/Url where the credential is used for</param>
        /// <returns>empty credentials if target not found, else stored credentials</returns>
        public static NetworkCredential GetCredentials(string Target)
        {
            IntPtr nCredPtr;
            var    username = String.Empty;
            var    passwd   = String.Empty;
            var    domain   = String.Empty;

            // Make the API call using the P/Invoke signature
            bool ret = NativeCode.CredRead(Target, NativeCode.CredentialType.Generic, 0, out nCredPtr);

            // If the API was successful then...
            if (ret)
            {
                using (CriticalCredentialHandle critCred = new CriticalCredentialHandle(nCredPtr))
                {
                    Credential cred = critCred.GetCredential();
                    passwd = cred.CredentialBlob;
                    var           user          = cred.UserName;
                    StringBuilder userBuilder   = new StringBuilder();
                    StringBuilder domainBuilder = new StringBuilder();
                    var           code          = NativeCode.CredUIParseUserName(user, userBuilder, int.MaxValue, domainBuilder, int.MaxValue);
                    //assuming invalid account name to be not meeting condition for CredUIParseUserName
                    //"The name must be in UPN or down-level format, or a certificate"
                    if (code == NativeCode.CredentialUIReturnCodes.InvalidAccountName)
                    {
                        userBuilder.Append(user);
                    }
                    username = userBuilder.ToString();
                    domain   = domainBuilder.ToString();
                }
            }
            return(new NetworkCredential(username, passwd, domain));
        }
Esempio n. 2
0
        /// <summary>
        /// Accepts credentials in a console window
        /// </summary>
        /// <param name="Target">A descriptive text for where teh credentials being asked are used for</param>
        /// <returns>NetworkCredential object containing the user name, </returns>
        public static NetworkCredential PromptForCredentialsConsole(string target)
        {
            var user     = String.Empty;
            var password = String.Empty;
            var domain   = String.Empty;

            // Setup the flags and variables
            StringBuilder userPassword = new StringBuilder(), userID = new StringBuilder();
            bool          save = true;

            NativeCode.CredentialUIFlags flags = NativeCode.CredentialUIFlags.CompleteUsername |
                                                 NativeCode.CredentialUIFlags.ExcludeCertificates;

            // Prompt the user
            NativeCode.CredentialUIReturnCodes returnCode =
                NativeCode.CredUICmdLinePromptForCredentials(target, IntPtr.Zero, 0, userID, 100, userPassword, 100,
                                                             ref save, flags);

            password = userPassword.ToString();

            StringBuilder userBuilder   = new StringBuilder();
            StringBuilder domainBuilder = new StringBuilder();

            returnCode = NativeCode.CredUIParseUserName(userID.ToString(), userBuilder, int.MaxValue, domainBuilder,
                                                        int.MaxValue);
            switch (returnCode)
            {
            case NativeCode.CredentialUIReturnCodes.Success:     // The username is valid.
                user   = userBuilder.ToString();
                domain = domainBuilder.ToString();
                break;

            case NativeCode.CredentialUIReturnCodes.InvalidAccountName:     // The username is not valid.
                user   = userID.ToString();
                domain = null;
                break;

            case NativeCode.CredentialUIReturnCodes.InsufficientBuffer:     // One of the buffers is too small.
                throw new OutOfMemoryException();

            case NativeCode.CredentialUIReturnCodes.InvalidParameter
                : // ulUserMaxChars or ulDomainMaxChars is zero OR userName, user, or domain is NULL.
                throw new ArgumentNullException("userName");
            }
            return(new NetworkCredential(user, password, domain));
        }
Esempio n. 3
0
        private static bool ParseUserName(string usernameBuf, int maxUserName, int maxDomain, out string user,
                                          out string domain)
        {
            StringBuilder userBuilder   = new StringBuilder();
            StringBuilder domainBuilder = new StringBuilder();

            user   = String.Empty;
            domain = String.Empty;

            var returnCode = NativeCode.CredUIParseUserName(usernameBuf, userBuilder, maxUserName, domainBuilder,
                                                            maxDomain);

            Debug.WriteLine(returnCode);
            switch (returnCode)
            {
            case NativeCode.CredentialUIReturnCodes.Success:     // The username is valid.
                user   = userBuilder.ToString();
                domain = domainBuilder.ToString();
                return(true);
            }
            return(false);
        }