Exemplo n.º 1
0
        /// <summary>
        /// Retrieves connection specific authentication information.
        /// </summary>
        /// <param name="phoneBookPath">Required. The full path (including filename) of the phone book containing the entry.</param>
        /// <param name="entryName">Required. The name of the entry whose credentials to retrieve.</param>
        /// <returns>A byte array containing the authentication information, otherwise a null reference (<b>Nothing</b> in Visual Basic).</returns>
        /// <exception cref="System.ArgumentException"><paramref name="phoneBookPath"/> or <paramref name="entryName"/> is an empty string or null reference (<b>Nothing</b> in Visual Basic).</exception>
        public byte[] GetCustomAuthData(string phoneBookPath, string entryName)
        {
            if (string.IsNullOrEmpty(phoneBookPath))
            {
                ThrowHelper.ThrowArgumentException("phoneBookPath", Resources.Argument_StringCannotBeNullOrEmpty);
            }

            if (string.IsNullOrEmpty(entryName))
            {
                ThrowHelper.ThrowArgumentException("entryName", Resources.Argument_StringCannotBeNullOrEmpty);
            }

            byte[] result = null;

            RasGetCustomAuthDataParams info = new RasGetCustomAuthDataParams();
            info.PhoneBookPath = phoneBookPath;
            info.EntryName = entryName;
            info.BufferSize = IntPtr.Zero;

            bool retry = false;

            do
            {
                try
                {
                    info.Address = Marshal.AllocHGlobal(info.BufferSize);

                    int ret = SafeNativeMethods.Instance.GetCustomAuthData(info);
                    if (ret == NativeMethods.ERROR_BUFFER_TOO_SMALL)
                    {
                        retry = true;
                    }
                    else if (ret == NativeMethods.SUCCESS)
                    {
                        int bufferSize = info.BufferSize.ToInt32();

                        if (bufferSize != 0)
                        {
                            result = new byte[bufferSize];
                            Marshal.Copy(info.Address, result, 0, bufferSize);
                        }

                        retry = false;
                    }
                    else
                    {
                        ThrowHelper.ThrowRasException(ret);
                    }
                }
                catch (EntryPointNotFoundException)
                {
                    ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
                }
                finally
                {
                    if (info.Address != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(info.Address);
                    }
                }
            }
            while (retry);

            return result;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Retrieves connection specific authentication information.
        /// </summary>
        /// <param name="value">An <see cref="RasGetCustomAuthDataParams"/> containing call data.</param>
        /// <returns>If the function succeeds, the return value is zero.</returns>
        /// <exception cref="System.ArgumentNullException"><paramref name="value"/> is a null reference (<b>Nothing</b> in Visual Basic).</exception>
        public int GetCustomAuthData(RasGetCustomAuthDataParams value)
        {
            if (value == null)
            {
                ThrowHelper.ThrowArgumentNullException("value");
            }

            IntPtr bufferSize = value.BufferSize;

            PInvokeCallTraceEvent evt = new PInvokeCallTraceEvent(NativeMethods.RasApi32Dll, "RasGetCustomAuthData");
            evt.Data.Add("phoneBookPath", value.PhoneBookPath);
            evt.Data.Add("entryName", value.EntryName);
            evt.Data.Add("address", value.Address);
            evt.Data.Add("bufferSize-IN", bufferSize);

            int result = 0;

            try
            {
                result = SafeNativeMethods.RasGetCustomAuthData(value.PhoneBookPath, value.EntryName, value.Address, ref bufferSize);
                evt.ResultCode = result;
                evt.Data.Add("bufferSize-OUT", bufferSize);

                value.BufferSize = bufferSize;
            }
            finally
            {
                DiagnosticTrace.Default.TraceEvent(TraceEventType.Verbose, evt);
            }

            return result;
        }