SelectAuthenticationScheme() private method

private SelectAuthenticationScheme ( HttpListenerContext context ) : AuthenticationSchemes
context HttpListenerContext
return AuthenticationSchemes
Example #1
0
        internal bool Authenticate()
        {
            var schm = _listener.SelectAuthenticationScheme(_request);

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

            if (schm == AuthenticationSchemes.None)
            {
                _response.Close(HttpStatusCode.Forbidden);
                return(false);
            }

            var realm = _listener.GetRealm();
            var user  =
                HttpUtility.CreateUser(
                    _request.Headers["Authorization"],
                    schm,
                    realm,
                    _request.HttpMethod,
                    _listener.GetUserCredentialsFinder()
                    );

            if (user == null || !user.Identity.IsAuthenticated)
            {
                _response.CloseWithAuthChallenge(new AuthenticationChallenge(schm, realm).ToString());
                return(false);
            }

            _user = user;
            return(true);
        }
Example #2
0
        internal bool Authenticate()
        {
            var schm = _listener.SelectAuthenticationScheme(_request);

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

            var basicAllowed  = HasFlag(schm, AuthenticationSchemes.Basic);
            var digestAllowed = HasFlag(schm, AuthenticationSchemes.Digest);

            if (!basicAllowed && !digestAllowed)
            {
                _response.Close(HttpStatusCode.Forbidden);
                return(false);
            }

            var realm = _listener.GetRealm();

            if (basicAllowed)
            {
                var user = HttpUtility.CreateUser(_request.Headers["Authorization"], AuthenticationSchemes.Basic, realm,
                                                  _request.HttpMethod, _listener.GetUserCredentialsFinder());
                if (user?.Identity?.IsAuthenticated == true)
                {
                    _user = user;
                    return(true);
                }
            }

            if (digestAllowed)
            {
                var user = HttpUtility.CreateUser(_request.Headers["Authorization"], AuthenticationSchemes.Digest, realm,
                                                  _request.HttpMethod, _listener.GetUserCredentialsFinder());
                if (user?.Identity?.IsAuthenticated == true)
                {
                    _user = user;
                    return(true);
                }
            }

            if (!digestAllowed)
            {
                _response.CloseWithAuthChallenge(AuthenticationChallenge.CreateBasicChallenge(realm).ToBasicString());
            }
            else
            {
                _response.CloseWithAuthChallenge(AuthenticationChallenge.CreateDigestChallenge(realm).ToDigestString());
            }

            return(false);
        }
        internal void Complete(HttpListenerContext context, bool syncCompleted)
        {
            HttpListener          listener = context.Listener;
            AuthenticationSchemes authenticationSchemes = listener.SelectAuthenticationScheme(context);

            if (authenticationSchemes == AuthenticationSchemes.None)
            {
                context.Response.Close(HttpStatusCode.Forbidden);
                listener.BeginGetContext(this);
                return;
            }
            string text = context.Request.Headers["Authorization"];

            if (authenticationSchemes == AuthenticationSchemes.Basic && (text == null || !text.StartsWith("basic", StringComparison.OrdinalIgnoreCase)))
            {
                context.Response.CloseWithAuthChallenge(HttpUtility.CreateBasicAuthChallenge(listener.Realm));
                listener.BeginGetContext(this);
                return;
            }
            if (authenticationSchemes == AuthenticationSchemes.Digest && (text == null || !text.StartsWith("digest", StringComparison.OrdinalIgnoreCase)))
            {
                context.Response.CloseWithAuthChallenge(HttpUtility.CreateDigestAuthChallenge(listener.Realm));
                listener.BeginGetContext(this);
                return;
            }
            this._context       = context;
            this._syncCompleted = syncCompleted;
            object sync = this._sync;

            lock (sync)
            {
                this._completed = true;
                if (this._waitHandle != null)
                {
                    this._waitHandle.Set();
                }
                if (this._callback != null)
                {
                    ThreadPool.UnsafeQueueUserWorkItem(new WaitCallback(ListenerAsyncResult.invokeCallback), this);
                }
            }
        }
        internal bool Authenticate()
        {
            var schm = _listener.SelectAuthenticationScheme(_request);

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

            if (schm == AuthenticationSchemes.None)
            {
                _errorStatusCode = 403;
                _errorMessage    = "Authentication not allowed";
                SendError();

                return(false);
            }

            var realm = _listener.GetRealm();
            var user  = HttpUtility.CreateUser(
                _request.Headers["Authorization"],
                schm,
                realm,
                _request.HttpMethod,
                _listener.GetUserCredentialsFinder()
                );

            if (user == null || !user.Identity.IsAuthenticated)
            {
                var chal = new AuthenticationChallenge(schm, realm).ToString();
                sendAuthenticationChallenge(chal);

                return(false);
            }

            _user = user;

            return(true);
        }