Exemple #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);
        }
Exemple #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 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);
        }