示例#1
0
        //Работает
        public AuthInfoWithSocket Auth(object data)
        {
            #region Тестовые данные

            /*
             * {
             *  "execFun": "authorization",
             *  "data": {
             *      "login": "******",
             *      "an": "00000000000000000000000000000000"
             *  }
             * }
             *
             */
            #endregion

            AuthInfoWithSocket output = new AuthInfoWithSocket();

            AuthInfo info;
            try
            {
                info = JsonConvert.DeserializeObject <AuthInfo>(data.ToString());

                using (var db = new CloudChatEntities())
                {
                    if (db.members.Any(it => it.login.Trim().Equals(info.login, StringComparison.CurrentCultureIgnoreCase) && it.an == info.an))
                    {
                        output.auth     = true;
                        output.publicId = db.members.FirstOrDefault(it => it.login.Trim().Equals(info.login, StringComparison.CurrentCultureIgnoreCase) && it.an == info.an).public_id;
                        output.an       = info.an;
                    }
                    else
                    {
                        output.auth = false;
                    }
                }
            }
            catch
            {
                output.auth = false;
            }
            return(output);
        }
示例#2
0
        private void WebSocket_NewMessageReceived(WebSocketSession session, string value)
        {
            OutputSocketMessage outputSocket;
            InputSocketMessage  socketMessage;

            //Преобразование входного сообщения
            try
            {
                socketMessage = JsonConvert.DeserializeObject <InputSocketMessage>(value);
            }
            catch
            {
                outputSocket = new OutputSocketMessage("DeserializeObject error:",
                                                       false,
                                                       String.Format("Input message: '{0}' is not valid", value),
                                                       new { }
                                                       );
                SendMessage(session, JsonConvert.SerializeObject(outputSocket));
                return;
            }

            //Авторизация
            if (socketMessage.execFun.Equals("authorization", StringComparison.CurrentCultureIgnoreCase))
            {
                AuthInfoWithSocket inf = (new MainAction()).Auth(socketMessage.data);
                if (inf.auth)
                {
                    inf.socket = session;
                    mClients.Add(inf);
                    outputSocket = new OutputSocketMessage(socketMessage.execFun, true, "", new { publicId = inf.publicId });
                    SendMessage(session, JsonConvert.SerializeObject(outputSocket));
                    return;
                }
                else
                {
                    outputSocket = new OutputSocketMessage(socketMessage.execFun,
                                                           false,
                                                           String.Format("Invalid login or AN"),
                                                           new { }
                                                           );
                    SendMessage(session, JsonConvert.SerializeObject(outputSocket));
                    return;
                }
            }
            else if (socketMessage.execFun.Equals("closesocket") && webSocket != null)
            {
                session.Send("Socket was closed");
                webSocket.Stop();
            }
            else
            {
                //Проверка - авторизован ли пользователь
                if (mClients.Any(it => it.socket == session) || socketMessage.execFun.Equals("registration", StringComparison.CurrentCultureIgnoreCase))
                {
                    MainAction refClass = new MainAction();

                    var me = refClass.GetType().GetMethod(socketMessage.execFun.ToLower(), System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
                    if (me != null)
                    {
                        AuthInfoWithSocket           currentUser = mClients.FirstOrDefault(it => it.socket == session);
                        OutputSocketMessageWithUsers response    = (OutputSocketMessageWithUsers)me.Invoke(refClass, new object[] { socketMessage.data, currentUser?.publicId });

                        outputSocket = response.msgForOwner;

                        SendMessage(session, JsonConvert.SerializeObject(outputSocket));

                        if (outputSocket.success)
                        {
                            Action <AuthInfoWithSocket> action = delegate(AuthInfoWithSocket s) { SendMessage(s.socket, JsonConvert.SerializeObject(response.msgForOther)); };
                            mClients.Where(it => response.usersId.Contains(it.publicId)).ToList().ForEach(action);
                        }
                    }
                    else
                    {
                        outputSocket = new OutputSocketMessage(socketMessage.execFun,
                                                               false,
                                                               String.Format("Function: '{0}' not found", socketMessage.execFun),
                                                               new { }
                                                               );
                        SendMessage(session, JsonConvert.SerializeObject(outputSocket));
                        return;
                    }
                }
                else
                {
                    outputSocket = new OutputSocketMessage(socketMessage.execFun,
                                                           false,
                                                           "You are not authorized. To continue working you need to login.",
                                                           new { }
                                                           );
                    SendMessage(session, JsonConvert.SerializeObject(outputSocket));
                    return;
                }
            }
        }