Exemplo n.º 1
0
        internal bool Authenticate(HttpListenerContext context)
        {
            var schm = SelectAuthenticationScheme(context);

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

            if (schm != AuthenticationSchemes.Basic && schm != AuthenticationSchemes.Digest)
            {
                context.Response.Close(HttpStatusCode.Forbidden);
                return(false);
            }

            var realm = Realm;
            var req   = context.Request;
            var user  =
                HttpUtility.CreateUser(
                    req.Headers["Authorization"], schm, realm, req.HttpMethod, UserCredentialsFinder
                    );

            if (user == null || !user.Identity.IsAuthenticated)
            {
                context.Response.CloseWithAuthChallenge(
                    new AuthenticationChallenge(schm, realm).ToString()
                    );

                return(false);
            }

            context.User = user;
            return(true);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        internal bool Authenticate()
        {
            bool flag;
            AuthenticationSchemes authenticationScheme = this._listener.SelectAuthenticationScheme(this._request);

            if (authenticationScheme == AuthenticationSchemes.Anonymous)
            {
                flag = true;
            }
            else if (authenticationScheme != AuthenticationSchemes.None)
            {
                string     realm     = this._listener.GetRealm();
                IPrincipal principal = HttpUtility.CreateUser(this._request.Headers["Authorization"], authenticationScheme, realm, this._request.HttpMethod, this._listener.GetUserCredentialsFinder());
                if ((principal == null ? false : principal.Identity.IsAuthenticated))
                {
                    this._user = principal;
                    flag       = true;
                }
                else
                {
                    this._response.CloseWithAuthChallenge((new AuthenticationChallenge(authenticationScheme, realm)).ToString());
                    flag = false;
                }
            }
            else
            {
                this._response.Close(HttpStatusCode.Forbidden);
                flag = false;
            }
            return(flag);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        internal bool AuthenticateContext(HttpListenerContext context)
        {
            var req  = context.Request;
            var schm = selectAuthenticationScheme(req);

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

            if (schm == AuthenticationSchemes.None)
            {
                context.ErrorStatusCode = 403;
                context.ErrorMessage    = "Authentication not allowed";

                context.SendError();

                return(false);
            }

            var realm = getRealm();
            var user  = HttpUtility.CreateUser(
                req.Headers["Authorization"],
                schm,
                realm,
                req.HttpMethod,
                _userCredFinder
                );

            var authenticated = user != null && user.Identity.IsAuthenticated;

            if (!authenticated)
            {
                context.SendAuthenticationChallenge(schm, realm);

                return(false);
            }

            context.User = user;

            return(true);
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        internal bool Authenticate()
        {
            AuthenticationSchemes scheme = this._listener.SelectAuthenticationScheme(this._request);

            if (scheme == AuthenticationSchemes.Anonymous)
            {
                return(true);
            }
            if (scheme == AuthenticationSchemes.None)
            {
                this._response.Close(HttpStatusCode.Forbidden);
                return(false);
            }
            string     realm     = this._listener.GetRealm();
            IPrincipal principal = HttpUtility.CreateUser(this._request.Headers["Authorization"], scheme, realm, this._request.HttpMethod, this._listener.GetUserCredentialsFinder());

            if ((principal != null) && principal.Identity.IsAuthenticated)
            {
                this._user = principal;
                return(true);
            }
            this._response.CloseWithAuthChallenge(new AuthenticationChallenge(scheme, realm).ToString());
            return(false);
        }
Exemplo n.º 8
0
        internal bool Authenticate(HttpListenerContext context)
        {
            AuthenticationSchemes authenticationSchemes = SelectAuthenticationScheme(context);

            switch (authenticationSchemes)
            {
            case AuthenticationSchemes.Anonymous:
                return(true);

            default:
                context.Response.Close(HttpStatusCode.Forbidden);
                return(false);

            case AuthenticationSchemes.Digest:
            case AuthenticationSchemes.Basic:
            {
                string realm = Realm;
                HttpListenerRequest request   = context.Request;
                IPrincipal          principal = HttpUtility.CreateUser(request.Headers["Authorization"], authenticationSchemes, realm, request.HttpMethod, UserCredentialsFinder);
                if (principal != null && principal.Identity.IsAuthenticated)
                {
                    context.User = principal;
                    return(true);
                }
                if (authenticationSchemes == AuthenticationSchemes.Basic)
                {
                    context.Response.CloseWithAuthChallenge(AuthenticationChallenge.CreateBasicChallenge(realm).ToBasicString());
                }
                if (authenticationSchemes == AuthenticationSchemes.Digest)
                {
                    context.Response.CloseWithAuthChallenge(AuthenticationChallenge.CreateDigestChallenge(realm).ToDigestString());
                }
                return(false);
            }
            }
        }
        private static bool authenticate(
            HttpListenerContext context,
            AuthenticationSchemes scheme,
            string realm,
            Func <IIdentity, NetworkCredential> credentialsFinder)
        {
            if (!(scheme == AuthenticationSchemes.Basic || scheme == AuthenticationSchemes.Digest))
            {
                context.Response.Close(HttpStatusCode.Forbidden);
                return(false);
            }

            var req  = context.Request;
            var user = HttpUtility.CreateUser(
                req.Headers["Authorization"], scheme, realm, req.HttpMethod, credentialsFinder);

            if (user != null && user.Identity.IsAuthenticated)
            {
                context.User = user;
                return(true);
            }

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

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

            return(false);
        }