コード例 #1
0
ファイル: RasHelper.cs プロジェクト: RSchwoerer/Terminals
        /// <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;
        }
コード例 #2
0
ファイル: RasEntry.cs プロジェクト: RSchwoerer/Terminals
        /// <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);
        }
コード例 #3
0
ファイル: RasEntry.cs プロジェクト: RSchwoerer/Terminals
        /// <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);
        }
コード例 #4
0
ファイル: RasHelperTest.cs プロジェクト: RSchwoerer/Terminals
        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);
        }
コード例 #5
0
ファイル: RasHelperTest.cs プロジェクト: RSchwoerer/Terminals
        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);
        }
コード例 #6
0
ファイル: RasHelperTest.cs プロジェクト: RSchwoerer/Terminals
        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);
        }
コード例 #7
0
ファイル: RasHelperTest.cs プロジェクト: RSchwoerer/Terminals
        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);
        }
コード例 #8
0
ファイル: RasHelperTest.cs プロジェクト: RSchwoerer/Terminals
        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);
        }