CreateDigestChallenge() static private method

static private CreateDigestChallenge ( string realm ) : AuthenticationChallenge
realm string
return AuthenticationChallenge
Exemplo n.º 1
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)
        {
            var listener = context.Listener;
            var scheme   = listener.SelectAuthenticationScheme(context);

            if (scheme == AuthenticationSchemes.None)
            {
                context.Response.Close(HttpStatusCode.Forbidden);
                listener.BeginGetContext(this);

                return;
            }

            var header = context.Request.Headers ["Authorization"];

            if (scheme == AuthenticationSchemes.Basic &&
                (header == null || !header.StartsWith("basic", StringComparison.OrdinalIgnoreCase)))
            {
                context.Response.CloseWithAuthChallenge(
                    AuthenticationChallenge.CreateBasicChallenge(listener.Realm).ToBasicString());

                listener.BeginGetContext(this);
                return;
            }

            if (scheme == AuthenticationSchemes.Digest &&
                (header == null || !header.StartsWith("digest", StringComparison.OrdinalIgnoreCase)))
            {
                context.Response.CloseWithAuthChallenge(
                    AuthenticationChallenge.CreateDigestChallenge(listener.Realm).ToDigestString());

                listener.BeginGetContext(this);
                return;
            }

            _context       = context;
            _syncCompleted = syncCompleted;

            lock (_sync) {
                _completed = true;
                if (_waitHandle != null)
                {
                    _waitHandle.Set();
                }

                if (_callback != null)
                {
                    ThreadPool.QueueUserWorkItem(invokeCallback, this);
                }
            }
        }
Exemplo n.º 3
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.User = user;
                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);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 6
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);
        }