private async Task <bool> AuthenticateRequest(AuthenticationSchemes scheme, TcpListenerWebSocketContext context)
        {
            var chal = scheme == AuthenticationSchemes.Basic
                       ? AuthenticationChallenge.CreateBasicChallenge(Realm).ToBasicString()
                       : scheme == AuthenticationSchemes.Digest
                         ? AuthenticationChallenge.CreateDigestChallenge(Realm).ToDigestString()
                         : null;

            if (chal == null)
            {
                await context.Close(HttpStatusCode.Forbidden).ConfigureAwait(false);

                return(false);
            }

            var retry                = -1;
            var schm                 = scheme.ToString();
            var realm                = Realm;
            var credFinder           = UserCredentialsFinder;
            Func <Task <bool> > auth = () => Task.FromResult(false);

            auth = async() =>
            {
                var auth1 = auth;
                retry++;
                if (retry > 99)
                {
                    await context.Close(HttpStatusCode.Forbidden).ConfigureAwait(false);

                    return(false);
                }

                var res = await context.GetHeader("Authorization").ConfigureAwait(false);

                if (res == null || !res.StartsWith(schm, StringComparison.OrdinalIgnoreCase))
                {
                    context.SendAuthenticationChallenge(chal);
                    return(await auth1().ConfigureAwait(false));
                }

                await context.SetUser(scheme, realm, credFinder).ConfigureAwait(false);

                if (!context.IsAuthenticated)
                {
                    context.SendAuthenticationChallenge(chal);
                    return(await auth1().ConfigureAwait(false));
                }

                return(true);
            };

            return(await auth().ConfigureAwait(false));
        }
示例#2
0
        private bool authenticateRequest(
            AuthenticationSchemes scheme, TcpListenerWebSocketContext context)
        {
            var chal = scheme == AuthenticationSchemes.Basic
                 ? AuthenticationChallenge.CreateBasicChallenge(Realm).ToBasicString()
                 : scheme == AuthenticationSchemes.Digest
                   ? AuthenticationChallenge.CreateDigestChallenge(Realm).ToDigestString()
                   : null;

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

            var         retry      = -1;
            var         schm       = scheme.ToString();
            var         realm      = Realm;
            var         credFinder = UserCredentialsFinder;
            Func <bool> auth       = null;

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

                var res = context.Headers["Authorization"];
                if (res == null || !res.StartsWith(schm, StringComparison.OrdinalIgnoreCase))
                {
                    context.SendAuthenticationChallenge(chal);
                    return(auth());
                }

                context.SetUser(scheme, realm, credFinder);
                if (!context.IsAuthenticated)
                {
                    context.SendAuthenticationChallenge(chal);
                    return(auth());
                }

                return(true);
            };

            return(auth());
        }
示例#3
0
        private static bool authenticate(
            TcpListenerWebSocketContext context,
            AuthenticationSchemes scheme,
            string realm,
            Func <IIdentity, NetworkCredential> credentialsFinder)
        {
            var chal = scheme == AuthenticationSchemes.Basic
                                           ? AuthenticationChallenge.CreateBasicChallenge(realm).ToBasicString()
                                           : scheme == AuthenticationSchemes.Digest
                                                 ? AuthenticationChallenge.CreateDigestChallenge(realm).ToDigestString()
                                                 : null;

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

            var         retry = -1;
            Func <bool> auth  = null;

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

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

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

                context.SendAuthenticationChallenge(chal);
                return(auth());
            };

            return(auth());
        }
示例#4
0
        private bool authenticateRequest(AuthenticationSchemes scheme, HttpListenerContext context)
        {
            if (context.Request.IsAuthenticated)
            {
                return(true);
            }

            if (scheme == AuthenticationSchemes.Basic)
            {
                context.Response.CloseWithAuthChallenge(
                    AuthenticationChallenge.CreateBasicChallenge(_listener.Realm).ToBasicString());
            }
            else if (scheme == AuthenticationSchemes.Digest)
            {
                context.Response.CloseWithAuthChallenge(
                    AuthenticationChallenge.CreateDigestChallenge(_listener.Realm).ToDigestString());
            }
            else
            {
                context.Response.Close(HttpStatusCode.Forbidden);
            }

            return(false);
        }
示例#5
0
        private static bool authenticate(TcpListenerWebSocketContext context, WebSocketSharp.Net.AuthenticationSchemes scheme, string realm, Func <IIdentity, WebSocketSharp.Net.NetworkCredential> credentialsFinder)
        {
            string chal = ((scheme == WebSocketSharp.Net.AuthenticationSchemes.Basic) ? AuthenticationChallenge.CreateBasicChallenge(realm).ToBasicString() : ((scheme != WebSocketSharp.Net.AuthenticationSchemes.Digest) ? null : AuthenticationChallenge.CreateDigestChallenge(realm).ToDigestString()));

            if (chal == null)
            {
                context.Close(WebSocketSharp.Net.HttpStatusCode.Forbidden);
                return(false);
            }
            int         retry = -1;
            Func <bool> auth  = null;

            auth = delegate
            {
                retry++;
                if (retry > 99)
                {
                    context.Close(WebSocketSharp.Net.HttpStatusCode.Forbidden);
                    return(false);
                }
                IPrincipal principal = HttpUtility.CreateUser(context.Headers["Authorization"], scheme, realm, context.HttpMethod, credentialsFinder);
                if (principal != null && principal.Identity.IsAuthenticated)
                {
                    context.SetUser(principal);
                    return(true);
                }
                context.SendAuthenticationChallenge(chal);
                return(auth());
            };
            return(auth());
        }