private bool authenticateRequest(
            AuthenticationSchemes scheme, HttpListenerContext context)
        {
            if (context.Request.IsAuthenticated)
            {
                return(true);
            }

            if (scheme == AuthenticationSchemes.Basic)
            {
                context.Response.CloseWithAuthChallenge(
                    HttpUtility.CreateBasicAuthChallenge(_listener.Realm));
            }
            else if (scheme == AuthenticationSchemes.Digest)
            {
                context.Response.CloseWithAuthChallenge(
                    HttpUtility.CreateDigestAuthChallenge(_listener.Realm));
            }
            else
            {
                context.Response.Close(HttpStatusCode.Forbidden);
            }

            return(false);
        }
        private bool authenticateRequest(
            AuthenticationSchemes authScheme, TcpListenerWebSocketContext context)
        {
            var challenge = authScheme == AuthenticationSchemes.Basic
                    ? HttpUtility.CreateBasicAuthChallenge(Realm)
                    : authScheme == AuthenticationSchemes.Digest
                      ? HttpUtility.CreateDigestAuthChallenge(Realm)
                      : null;

            if (challenge == null)
            {
                context.Close(HttpStatusCode.Forbidden);
                return(false);
            }

            var         retry             = -1;
            var         expected          = authScheme.ToString();
            var         realm             = Realm;
            var         credentialsFinder = UserCredentialsFinder;
            Func <bool> auth = null;

            auth = () => {
                retry++;
                if (retry > 99)
                {
                    context.Close(HttpStatusCode.Forbidden);
                    return(false);
                }

                var header = context.Headers ["Authorization"];
                if (header == null ||
                    !header.StartsWith(expected, StringComparison.OrdinalIgnoreCase))
                {
                    context.SendAuthChallenge(challenge);
                    return(auth());
                }

                context.SetUser(authScheme, realm, credentialsFinder);
                if (context.IsAuthenticated)
                {
                    return(true);
                }

                context.SendAuthChallenge(challenge);
                return(auth());
            };

            return(auth());
        }
Beispiel #3
0
        private bool authenticateRequest(WebSocketSharp.Net.AuthenticationSchemes scheme, TcpListenerWebSocketContext context)
        {
            string challenge = (scheme != WebSocketSharp.Net.AuthenticationSchemes.Basic) ? ((scheme != WebSocketSharp.Net.AuthenticationSchemes.Digest) ? null : HttpUtility.CreateDigestAuthChallenge(this.Realm)) : HttpUtility.CreateBasicAuthChallenge(this.Realm);

            if (challenge == null)
            {
                context.Close(WebSocketSharp.Net.HttpStatusCode.Forbidden);
                return(false);
            }
            int    retry    = -1;
            string expected = scheme.ToString();
            string realm    = this.Realm;
            Func <IIdentity, WebSocketSharp.Net.NetworkCredential> credentialsFinder = this.UserCredentialsFinder;
            Func <bool> auth = null;

            auth = delegate()
            {
                retry++;
                if (retry > 99)
                {
                    context.Close(WebSocketSharp.Net.HttpStatusCode.Forbidden);
                    return(false);
                }
                string text = context.Headers["Authorization"];
                if (text == null || !text.StartsWith(expected, StringComparison.OrdinalIgnoreCase))
                {
                    context.SendAuthChallenge(challenge);
                    return(auth());
                }
                context.SetUser(scheme, realm, credentialsFinder);
                if (context.IsAuthenticated)
                {
                    return(true);
                }
                context.SendAuthChallenge(challenge);
                return(auth());
            };
            return(auth());
        }