void getCredentails()
        {
            try
            {
                if (!string.IsNullOrEmpty(encryptedData))
                {
                    var xml = PasswordHepler.GetFullString(encryptedData, Id, emgName);

                    if (!string.IsNullOrEmpty(xml))
                    {
                        //read the xml
                        if (!xml.EndsWith("/>"))
                        {
                            xml = xml.Substring(0, xml.IndexOf("/>") + 2);
                        }

                        XmlDocument xmlDoc = new XmlDocument();
                        xmlDoc.Load(new System.IO.StringReader(xml));
                        XPathNavigator xmlNav = xmlDoc.CreateNavigator();
                        xmlNav.MoveToFirstChild();
                        xmlNav.MoveToFirstAttribute();

                        do
                        {
                            if (xmlNav.Name == "Username")
                            {
                                UserName         = xmlNav.Value;
                                txtUsername.Text = UserName;
                            }
                            if (xmlNav.Name == "Domain")
                            {
                                Domain        = xmlNav.Value;
                                cbDomain.Text = Domain;
                            }
                            if (xmlNav.Name == "Password")
                            {
                                if (Password == null)
                                {
                                    Password = new SecureString();
                                }
                                foreach (char c in xmlNav.Value)
                                {
                                    Password.AppendChar(c);
                                }

                                pbPassword.Password = xmlNav.Value;
                            }
                        } while (xmlNav.MoveToNextAttribute());
                    }
                }
            }
            catch { }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sets or Clears the internal secure string used for the password.
        /// If an Empty String is passed ("") the secure password is cleared. If a none-zero length string is passed then the password is set.
        /// The Password will only be used by a process if the User Name and Domain is specified and it is not an Empty or null string (i.e.,Length &gt; 0)
        /// it is recommend that the working directory also be specified else the working directory will be %SYSTEMROOT%\system32
        /// </summary>
        /// <remarks>https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.password(v=vs.110).aspx</remarks>
        /// <param name="pwd">Process Password</param>
        /// <returns>true if password is set or cleared otherwise false</returns>
        /// <seealso cref="System.Diagnostics.ProcessStartInfo.Password"/>
        /// <seealso cref="System.Security.SecureString"/>
        public Boolean SetPassword(string pwd)
        {
            try
            {
                if (pwd == String.Empty && Password.Length > 0)
                {
                    Password.Clear();
                    return(true);
                }
                else if (pwd.Length > 0)
                {
                    if (Password.Length > 0)
                    {
                        Password.Clear();
                    }

                    for (int i = 0; i < pwd.Length; i++)
                    {
                        Password.AppendChar(pwd.ElementAt <char>(i));
                    }

                    return(true);
                }
            }
            catch (CryptographicException ex)
            {
                SetLastError(string.Format("Crytopgraphic Exception: {0}", ex.Message), ex.InnerException);
            }
            catch (InvalidOperationException ex)
            {
                SetLastError(string.Format("Secure String is Read Only: {0}", ex.Message));
            }
            catch (ArgumentOutOfRangeException ex)
            {
                SetLastError(string.Format("String is too long (i.e., > 65,536 characters): {0}", ex.Message));
            }
            catch (Exception ex)
            {
                SetLastError(ex.Message, ex.InnerException);
            }
            return(false);
        }
Exemplo n.º 3
0
        public DialogResult ShowDialog(IWin32Window owner)
        {
            if (String.IsNullOrEmpty(_targetName))
            {
                throw new ArgumentNullException("TargetName");
            }

            DialogResult        result;
            int                 passwordMaxLength = CREDUI_MAX_PASSWORD_LENGTH + 1;
            StringBuilder       username          = new StringBuilder(_userName, CREDUI_MAX_USERNAME_LENGTH);
            SafeBitmapHandle    banner            = _banner != null ? new SafeBitmapHandle(_banner.GetHbitmap(), true) : new SafeBitmapHandle(IntPtr.Zero, false);
            SafeUnmanagedBuffer password          = new SafeUnmanagedBuffer(passwordMaxLength * sizeof(char));

            for (int i = 0; i < sizeof(char); i++)
            {
                password[i] = 0;
            }

            CheckNotDisposed();

            var info = new NativeMethods.CREDUI_INFO()
            {
                hwndParent     = owner != null ? owner.Handle : IntPtr.Zero,
                hbmBanner      = banner.DangerousGetHandle(),
                pszCaptionText = _caption,
                pszMessageText = _message
            };

            info.cbSize = Marshal.SizeOf(info);

            try
            {
                var nativeResult = NativeMethods.CredUIPromptForCredentials(ref info, _targetName, IntPtr.Zero, 0,
                                                                            username, CRED_MAX_USERNAME_LENGTH,
                                                                            new HandleRef(password, password.DangerousGetHandle()), passwordMaxLength,
                                                                            ref _saveChecked, _options);

                /*IntPtr outBuffer;
                 * uint authPackage = 0;
                 * uint outBufferSize;
                 * info.hbmBanner = IntPtr.Zero;
                 * nativeResult = NativeMethods.CredUIPromptForWindowsCredentials(ref info, 0, ref authPackage, new HandleRef(null, IntPtr.Zero), 0, out outBuffer, out outBufferSize, ref _saveChecked,
                 *  0x1 | 0x200);
                 *
                 * int nameLen = 0, passLen = 0, dNameLength = 0;
                 * NativeMethods.CredUnPackAuthenticationBuffer(0, outBuffer, (int)outBufferSize, null, ref nameLen, null, ref dNameLength, null, ref passLen);
                 * StringBuilder sName = new StringBuilder(nameLen), sPass = new StringBuilder(passLen), dName = new StringBuilder(dNameLength);
                 * bool pos = NativeMethods.CredUnPackAuthenticationBuffer(0, outBuffer, (int)outBufferSize, sName, ref nameLen,
                 *  dName, ref dNameLength, sPass, ref passLen);
                 *
                 *
                 * var b = new SafeCoUnmanagedBuffer(outBuffer, checked((int)outBufferSize), true);
                 *
                 * string s = Marshal.PtrToStringUni(outBuffer);*/

                switch (nativeResult)
                {
                case NativeMethods.CredUIReturnCodes.NO_ERROR:
                    _userName = username.ToString();

                    Password.Clear();
                    byte[] unicodeBytes = new byte[sizeof(char)];
                    char   currentChar  = Char.MaxValue;
                    for (int i = 0; ;)
                    {
                        for (int j = 0; j < sizeof(char); j++)
                        {
                            unicodeBytes[j] = password[i];
                            password[i++]   = 0;
                        }

                        if ((currentChar = Encoding.Unicode.GetChars(unicodeBytes)[0]) != '\0')
                        {
                            Password.AppendChar(currentChar);
                        }
                        else
                        {
                            break;
                        }
                    }

                    result = DialogResult.OK;
                    break;

                case NativeMethods.CredUIReturnCodes.ERROR_CANCELLED:
                    result = DialogResult.Cancel;
                    break;

                default:
                    throw new Win32Exception((int)nativeResult);
                }
            }
            finally
            {
                banner.Dispose();
                password.Dispose();
            }

            return(result);
        }