Beispiel #1
0
        /// <summary>
        /// Retrieves user credentials associated with a specified remote access phone book entry.
        /// </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>
        /// <param name="options">The options to request.</param>
        /// <returns>The credentials stored in the entry, otherwise a null reference (<b>Nothing</b> in Visual Basic) if the credentials did not exist.</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>
        /// <exception cref="System.UnauthorizedAccessException">The caller does not have the required permission to perform the action requested.</exception>
        public NetworkCredential GetCredentials(string phoneBookPath, string entryName, NativeMethods.RASCM options)
        {
            if (string.IsNullOrEmpty(phoneBookPath))
            {
                ThrowHelper.ThrowArgumentException("phoneBookPath", Resources.Argument_StringCannotBeNullOrEmpty);
            }

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

            NetworkCredential retval = null;

            int size = Marshal.SizeOf(typeof(NativeMethods.RASCREDENTIALS));

            NativeMethods.RASCREDENTIALS credentials = new NativeMethods.RASCREDENTIALS();
            credentials.size = size;
            credentials.options = options;

            IntPtr pCredentials = IntPtr.Zero;
            try
            {
                pCredentials = Marshal.AllocHGlobal(size);
                Marshal.StructureToPtr(credentials, pCredentials, true);

                try
                {
                    int ret = UnsafeNativeMethods.Instance.GetCredentials(phoneBookPath, entryName, pCredentials);
                    if (ret == NativeMethods.SUCCESS)
                    {
                        credentials = Utilities.PtrToStructure<NativeMethods.RASCREDENTIALS>(pCredentials);
                        if (credentials.options != NativeMethods.RASCM.None)
                        {
                            retval = new NetworkCredential(
                                credentials.userName,
                                credentials.password,
                                credentials.domain);
                        }
                    }
                    else if (ret != NativeMethods.ERROR_FILE_NOT_FOUND)
                    {
                        ThrowHelper.ThrowRasException(ret);
                    }
                }
                catch (EntryPointNotFoundException)
                {
                    ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
                }
            }
            finally
            {
                if (pCredentials != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pCredentials);
                }
            }

            return retval;
        }
Beispiel #2
0
        /// <summary>
        /// Clears the stored credentials for the entry.
        /// </summary>
        /// <returns><b>true</b> if the operation was successful, otherwise <b>false</b>.</returns>
        /// <exception cref="System.InvalidOperationException">The entry is not associated with a phone book.</exception>
        public bool ClearCredentials()
        {
            if (this.Owner == null)
            {
                ThrowHelper.ThrowInvalidOperationException(Resources.Exception_EntryNotInPhoneBook);
            }

            NativeMethods.RASCREDENTIALS credentials = new NativeMethods.RASCREDENTIALS();
            credentials.userName = string.Empty;
            credentials.password = string.Empty;
            credentials.domain = string.Empty;

            credentials.options = NativeMethods.RASCM.UserName | NativeMethods.RASCM.Password | NativeMethods.RASCM.Domain;

            return RasHelper.Instance.SetCredentials(this.Owner.Path, this.Name, credentials, true);
        }
Beispiel #3
0
        /// <summary>
        /// Updates the user credentials for the entry.
        /// </summary>
        /// <param name="key">The pre-shared key to update.</param>
        /// <param name="value">The value to set.</param>
        /// <returns><b>true</b> if the operation was successful, otherwise <b>false</b>.</returns>
        /// <remarks>
        /// <para>
        /// <b>Windows XP and later: This method is supported.</b>
        /// </para>
        /// </remarks>
        public bool UpdateCredentials(RasPreSharedKey key, string value)
        {
            if (this.Owner == null)
            {
                ThrowHelper.ThrowInvalidOperationException(Resources.Exception_EntryNotInPhoneBook);
            }

            NativeMethods.RASCREDENTIALS credentials = new NativeMethods.RASCREDENTIALS();
            credentials.password = value;

            switch (key)
            {
                case RasPreSharedKey.Client:
                    credentials.options = NativeMethods.RASCM.PreSharedKey;
                    break;

                case RasPreSharedKey.Server:
                    credentials.options = NativeMethods.RASCM.ServerPreSharedKey;
                    break;

                case RasPreSharedKey.Ddm:
                    credentials.options = NativeMethods.RASCM.DdmPreSharedKey;
                    break;
            }

            return RasHelper.Instance.SetCredentials(this.Owner.Path, this.Name, credentials, false);
        }
Beispiel #4
0
        public void SetCredentialsNotSupportedTest()
        {
            Mock<IUnsafeNativeMethods> mock = new Mock<IUnsafeNativeMethods>();
            UnsafeNativeMethods.Instance = mock.Object;

            mock.Setup(o => o.SetCredentials(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<IntPtr>(), It.IsAny<bool>())).Throws<EntryPointNotFoundException>();

            NativeMethods.RASCREDENTIALS credentials = new NativeMethods.RASCREDENTIALS()
            {
                userName = "******",
                password = "******",
                domain = "Domain"
            };

            RasHelper.Instance.SetCredentials("C:\\Test.pbk", "Test Entry", credentials, false);
        }
Beispiel #5
0
        public void SetCredentialsUnexpectedResultTest()
        {
            Mock<IUnsafeNativeMethods> mock = new Mock<IUnsafeNativeMethods>();
            UnsafeNativeMethods.Instance = mock.Object;

            mock.Setup(o => o.SetCredentials(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<IntPtr>(), It.IsAny<bool>())).Returns(NativeMethods.ERROR_CANNOT_FIND_PHONEBOOK_ENTRY);

            NativeMethods.RASCREDENTIALS credentials = new NativeMethods.RASCREDENTIALS()
            {
                userName = "******",
                password = "******",
                domain = "Domain"
            };

            RasHelper.Instance.SetCredentials("C:\\Test.pbk", "Test Entry", credentials, false);
        }
Beispiel #6
0
        public void SetCredentialsTest()
        {
            bool expected = true;

            Mock<IUnsafeNativeMethods> mock = new Mock<IUnsafeNativeMethods>();
            UnsafeNativeMethods.Instance = mock.Object;

            mock.Setup(o => o.SetCredentials(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<IntPtr>(), It.IsAny<bool>())).Returns(NativeMethods.SUCCESS);

            NativeMethods.RASCREDENTIALS credentials = new NativeMethods.RASCREDENTIALS()
            {
                userName = "******",
                password = "******",
                domain = "Domain"
            };

            bool actual = RasHelper.Instance.SetCredentials("C:\\Test.pbk", "Test Entry", credentials, false);

            Assert.AreEqual(expected, actual);
        }
Beispiel #7
0
        public void SetCredentialsUnauthorizedExceptionTest()
        {
            Mock<IUnsafeNativeMethods> mock = new Mock<IUnsafeNativeMethods>();
            UnsafeNativeMethods.Instance = mock.Object;

            mock.Setup(o => o.SetCredentials(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<IntPtr>(), It.IsAny<bool>())).Returns(NativeMethods.ERROR_ACCESS_DENIED);

            NativeMethods.RASCREDENTIALS credentials = new NativeMethods.RASCREDENTIALS()
            {
                userName = "******",
                password = "******",
                domain = "Domain"
            };

            bool actual = RasHelper.Instance.SetCredentials("C:\\Test.pbk", "Test Entry", credentials, false);
        }
Beispiel #8
0
        public void GetCredentialsTest()
        {
            NativeMethods.RASCREDENTIALS expected = new NativeMethods.RASCREDENTIALS()
            {
                userName = "******",
                password = "******",
                domain = "Domain",
                options = NativeMethods.RASCM.UserName | NativeMethods.RASCM.Password | NativeMethods.RASCM.Domain
            };

            CopyStructToAddrMock<NativeMethods.RASCREDENTIALS> target = new CopyStructToAddrMock<NativeMethods.RASCREDENTIALS>();
            target.Result = expected;

            Mock<IUnsafeNativeMethods> mock = new Mock<IUnsafeNativeMethods>();
            UnsafeNativeMethods.Instance = mock.Object;

            mock.Setup(o => o.GetCredentials(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<IntPtr>())).Callback((string phoneBookPath, string entryName, IntPtr credentials) =>
                {
                    target.Execute(credentials);
                }).Returns(NativeMethods.SUCCESS);

            NetworkCredential actual = RasHelper.Instance.GetCredentials("C:\\Test.pbk", "Test Entry", NativeMethods.RASCM.UserName | NativeMethods.RASCM.Password | NativeMethods.RASCM.Domain);

            Assert.AreEqual(expected.userName, actual.UserName);
            Assert.AreEqual(expected.password, actual.Password);
            Assert.AreEqual(expected.domain, actual.Domain);
        }