Exemplo n.º 1
0
        /// <summary>
        /// Initiates an asynchronous operation to authenticate user credentials.
        /// </summary>
        /// <param name="realm">Specifies the authentication scope.</param>
        /// <param name="account">The account.</param>
        /// <param name="password">The password.</param>
        /// <param name="callback">The delegate to be called when the operation completes (or <c>null</c>).</param>
        /// <param name="state">Application defined state (or <c>null</c>).</param>
        /// <returns>The <see cref="IAsyncResult" /> instance to be used to track the operation.</returns>
        /// <remarks>
        /// <note>
        /// All successful calls to <see cref="BeginAuthenticate" /> must eventually be
        /// matched by a call to <see cref="EndAuthenticate" />.
        /// </note>
        /// </remarks>
        public IAsyncResult BeginAuthenticate(string realm, string account, string password, AsyncCallback callback, object state)
        {
            AsyncResult     arAuth = new AsyncResult(null, callback, state);
            AuthTransaction transaction;
            RadiusPacket    packet;
            string          userName;
            int             ID;
            IPEndPoint      binding;

            using (TimedLock.Lock(this))
            {
                userName = Helper.GetUserName(realmFormat, realm, account);

                ID = GetTransactionID();
                if (ID == -1)
                {
                    throw new RadiusException("RADIUS client cannot track more than 256 simultaneous authentication requests.");
                }

                binding = GetServerBinding(ref serverPos);
                if (binding == NetworkBinding.Any)
                {
                    throw new RadiusException("None of the RADIUS server hosts resolve to an IP address.");
                }

                packet = new RadiusPacket(RadiusCode.AccessRequest, ID, Crypto.Rand(16));
                packet.Attributes.Add(new RadiusAttribute(RadiusAttributeType.UserName, userName));
                packet.Attributes.Add(new RadiusAttribute(RadiusAttributeType.UserPassword, packet.EncryptUserPassword(password, secret)));
                packet.Attributes.Add(new RadiusAttribute(RadiusAttributeType.NasIpAddress, nasIPAddress));

                transaction      = new AuthTransaction(userName, packet, serverPos, SysTime.Now + retryInterval, arAuth);;
                transactions[ID] = transaction;
                sock.SendTo(transaction.Packet.ToArray(), binding);

                arAuth.Result = null;
                arAuth.Started();
            }

            return(arAuth);
        }