Пример #1
0
        public HttpListenerContext EndGetContext(IAsyncResult asyncResult)
        {
            this.CheckDisposed();
            if (asyncResult == null)
            {
                throw new ArgumentNullException("asyncResult");
            }
            ListenerAsyncResult listenerAsyncResult = asyncResult as ListenerAsyncResult;

            if (listenerAsyncResult == null)
            {
                throw new ArgumentException("Wrong IAsyncResult.", "asyncResult");
            }
            if (listenerAsyncResult.EndCalled)
            {
                throw new InvalidOperationException("Cannot reuse this IAsyncResult.");
            }
            listenerAsyncResult.EndCalled = true;
            if (!listenerAsyncResult.IsCompleted)
            {
                listenerAsyncResult.AsyncWaitHandle.WaitOne();
            }
            object syncRoot = ((ICollection)this._waitQueue).SyncRoot;

            lock (syncRoot)
            {
                int num = this._waitQueue.IndexOf(listenerAsyncResult);
                if (num >= 0)
                {
                    this._waitQueue.RemoveAt(num);
                }
            }
            HttpListenerContext   context = listenerAsyncResult.GetContext();
            AuthenticationSchemes authenticationSchemes = this.SelectAuthenticationScheme(context);

            if (authenticationSchemes != AuthenticationSchemes.Anonymous)
            {
                context.SetUser(authenticationSchemes, this.Realm, this.UserCredentialsFinder);
            }
            return(context);
        }
Пример #2
0
        private static bool authenticate(HttpListenerContext context)
        {
            var listener = context.Listener;
            var schm     = listener.SelectAuthenticationScheme(context);

            if (schm == AuthenticationSchemes.Anonymous)
            {
                return(true);
            }

            var req = context.Request;

            if (schm == AuthenticationSchemes.Basic)
            {
                var authRes = req.Headers["Authorization"];
                if (authRes == null || !authRes.StartsWith("basic", StringComparison.OrdinalIgnoreCase))
                {
                    context.Response.CloseWithAuthChallenge(
                        AuthenticationChallenge.CreateBasicChallenge(listener.Realm).ToBasicString());

                    return(false);
                }
            }
            else if (schm == AuthenticationSchemes.Digest)
            {
                var authRes = req.Headers["Authorization"];
                if (authRes == null || !authRes.StartsWith("digest", StringComparison.OrdinalIgnoreCase))
                {
                    context.Response.CloseWithAuthChallenge(
                        AuthenticationChallenge.CreateDigestChallenge(listener.Realm).ToDigestString());

                    return(false);
                }
            }
            else
            {
                context.Response.Close(HttpStatusCode.Forbidden);
                return(false);
            }

            var realm = listener.Realm;

            context.SetUser(schm, realm, listener.UserCredentialsFinder);
            if (req.IsAuthenticated)
            {
                return(true);
            }

            if (schm == AuthenticationSchemes.Basic)
            {
                context.Response.CloseWithAuthChallenge(
                    AuthenticationChallenge.CreateBasicChallenge(realm).ToBasicString());
            }

            if (schm == AuthenticationSchemes.Digest)
            {
                context.Response.CloseWithAuthChallenge(
                    AuthenticationChallenge.CreateDigestChallenge(realm).ToDigestString());
            }

            return(false);
        }