Example #1
0
        /// <summary>
        /// Returns an equivalent NetworkCredential object for this
        /// PSCredential.
        ///
        /// A null is returned if
        /// -- current object has not been initialized
        /// -- current creds are not compatible with NetworkCredential
        ///    (such as smart card creds or cert creds)
        /// </summary>
        ///
        /// <returns>
        ///     null if the current object has not been initialized.
        ///     null if the current credentials are incompatible with
        ///       a NetworkCredential -- such as smart card credentials.
        ///     the appropriate network credential for this PSCredential otherwise.
        /// </returns>
        public NetworkCredential GetNetworkCredential()
        {
            if (_netCred == null)
            {
                string user   = null;
                string domain = null;

                if (IsValidUserName(_userName, out user, out domain))
                {
#if CORECLR
                    // NetworkCredential constructor only accepts plain string password in CoreCLR
                    // Since user can already access the plain text password via PSCredential.GetNetworkCredential().Password,
                    // this change won't be a security issue for PS on CSS.
                    IntPtr unmanagedPtr = IntPtr.Zero;
                    try
                    {
                        unmanagedPtr = ClrFacade.SecureStringToCoTaskMemUnicode(_password);
                        string pwdInPlainText = System.Runtime.InteropServices.Marshal.PtrToStringUni(unmanagedPtr);
                        _netCred = new NetworkCredential(user, pwdInPlainText, domain);
                    }
                    finally
                    {
                        if (unmanagedPtr != IntPtr.Zero)
                        {
                            Marshal.ZeroFreeCoTaskMemUnicode(unmanagedPtr);
                        }
                    }
#else
                    _netCred = new NetworkCredential(user, _password, domain);
#endif
                }
            }

            return(_netCred);
        }
Example #2
0
        /// <summary>
        /// Returns an equivalent NetworkCredential object for this
        /// PSCredential.
        ///
        /// A null is returned if
        /// -- current object has not been initialized
        /// -- current creds are not compatible with NetworkCredential
        ///    (such as smart card creds or cert creds)
        /// </summary>
        ///
        /// <returns>
        ///     null if the current object has not been initialized.
        ///     null if the current credentials are incompatible with
        ///       a NetworkCredential -- such as smart card credentials.
        ///     the appropriate network credential for this PSCredential otherwise.
        /// </returns>
        public NetworkCredential GetNetworkCredential()
        {
            if (_netCred == null)
            {
                string user   = null;
                string domain = null;

                if (IsValidUserName(_userName, out user, out domain))
                {
#if CORECLR
                    // NetworkCredential constructor only accepts plain string password in .NET Core.
                    // TODO: This raises security concerns about having the plain string password in memory
                    // for an indefinite period of time. So we need to change back to the constructor that
                    // takes a SecureString password once it becomes available in .NET Core.
                    IntPtr unmanagedPtr = IntPtr.Zero;
                    try
                    {
                        unmanagedPtr = ClrFacade.SecureStringToCoTaskMemUnicode(_password);
                        string pwdInPlainText = System.Runtime.InteropServices.Marshal.PtrToStringUni(unmanagedPtr);
                        _netCred = new NetworkCredential(user, pwdInPlainText, domain);
                    }
                    finally
                    {
                        if (unmanagedPtr != IntPtr.Zero)
                        {
                            Marshal.ZeroFreeCoTaskMemUnicode(unmanagedPtr);
                        }
                    }
#else
                    _netCred = new NetworkCredential(user, _password, domain);
#endif
                }
            }

            return(_netCred);
        }