コード例 #1
0
        public void SendMessage(MessageBase message)
        {
            var webSocket = Context.WebSocket;

            if (webSocket.State == WebSocketState.Open)
            {
                var buffer = message.GetBytes();
                webSocket.SendAsync(new ArraySegment <byte>(buffer), WebSocketMessageType.Binary, true, CancellationToken.None);
            }
        }
コード例 #2
0
 public void Send(MessageBase messageBase)
 {
     // If the registrant exists
     if (Socket.Connected)
     {
         var data = messageBase.GetBytes();
         var task = Task.Factory.FromAsync <Int32>(Socket.BeginSend(data, 0, data.Length, SocketFlags.None, null, Socket), Socket.EndSend);
         task.ContinueWith(nextTask => Task_OnSendCompleted(task.Result, data.Length, Socket.RemoteEndPoint, messageBase.MessageType), TaskContinuationOptions.OnlyOnRanToCompletion);
     }
 }
コード例 #3
0
ファイル: NtlmClient.cs プロジェクト: pmq20/mono_forked
        public Authorization Authenticate(string challenge, WebRequest webRequest, ICredentials credentials)
        {
            HttpWebRequest request = webRequest as HttpWebRequest;

            if (request == null)
            {
                return(null);
            }

            NetworkCredential cred = credentials.GetCredential(request.RequestUri, "NTLM");

            if (cred == null)
            {
                return(null);
            }

            string userName = cred.UserName;
            string domain   = cred.Domain;
            string password = cred.Password;

            if (userName == null || userName == "")
            {
                return(null);
            }
            domain = domain != null && domain.Length > 0 ? domain : request.Headers ["Host"];

            bool completed = false;

            if (message == null)
            {
                Type1Message type1 = new Type1Message();
                type1.Domain = domain;
                message      = type1;
            }
            else if (message.Type == 1)
            {
                // Should I check the credentials?
                if (challenge == null)
                {
                    message = null;
                    return(null);
                }

                Type2Message type2 = new Type2Message(Convert.FromBase64String(challenge));
                if (password == null)
                {
                    password = "";
                }

                Type3Message type3 = new Type3Message();
                type3.Domain    = domain;
                type3.Username  = userName;
                type3.Challenge = type2.Nonce;
                type3.Password  = password;
                message         = type3;
                completed       = true;
            }
            else
            {
                // Should I check the credentials?
                // type must be 3 here
                if (challenge == null || challenge == String.Empty)
                {
                    Type1Message type1 = new Type1Message();
                    type1.Domain = domain;
                    message      = type1;
                }
                else
                {
                    completed = true;
                }
            }

            string token = "NTLM " + Convert.ToBase64String(message.GetBytes());

            return(new Authorization(token, completed));
        }
コード例 #4
0
        public Authorization Authenticate(string challenge, WebRequest webRequest, ICredentials credentials)
        {
            HttpWebRequest request = webRequest as HttpWebRequest;

            if (request == null)
            {
                return(null);
            }

            NetworkCredential cred = credentials.GetCredential(request.RequestUri, "NTLM");

            if (cred == null)
            {
                return(null);
            }

            string userName = cred.UserName;
            string domain   = cred.Domain;
            string password = cred.Password;

            if (userName == null || userName == "")
            {
                return(null);
            }

            if (String.IsNullOrEmpty(domain))
            {
                int idx = userName.IndexOf('\\');
                if (idx == -1)
                {
                    idx = userName.IndexOf('/');
                }
                if (idx >= 0)
                {
                    domain   = userName.Substring(0, idx);
                    userName = userName.Substring(idx + 1);
                }
            }

            bool completed = false;

            if (message == null)
            {
                Type1Message type1 = new Type1Message();
                type1.Domain = domain;
                type1.Host   = "";               // MS does not send it
                type1.Flags |= NtlmFlags.NegotiateNtlm2Key;
                message      = type1;
            }
            else if (message.Type == 1)
            {
                // Should I check the credentials?
                if (challenge == null)
                {
                    message = null;
                    return(null);
                }

                Type2Message type2 = new Type2Message(Convert.FromBase64String(challenge));
                if (password == null)
                {
                    password = "";
                }

                Type3Message type3 = new Type3Message(type2);
                type3.Username = userName;
                type3.Password = password;
                message        = type3;
                completed      = true;
            }
            else
            {
                // Should I check the credentials?
                // type must be 3 here
                if (challenge == null || challenge == String.Empty)
                {
                    Type1Message type1 = new Type1Message();
                    type1.Domain = domain;
                    type1.Host   = "";                   // MS does not send it
                    message      = type1;
                }
                else
                {
                    completed = true;
                }
            }

            string token = "NTLM " + Convert.ToBase64String(message.GetBytes());

            return(new Authorization(token, completed));
        }