private void asyncLoginAsync()
 {
     using (var client = new HttpClient())
     {
         string         passwordHashed           = Encoding.ASCII.GetString(new SHA256Managed().ComputeHash(Encoding.ASCII.GetBytes(passtxt.Text)));
         LoginGamerInfo lgInfo                   = new LoginGamerInfo(unametxt.Text, passwordHashed, remMeChecked);
         MessageWrapper <LoginGamerInfo> msgInfo = new MessageWrapper <LoginGamerInfo>()
         {
             Message = lgInfo
         };
         string sendingLink = JsonConvert.SerializeObject(msgInfo);
         Task <HttpResponseMessage> responseTask = client.PostAsync(uri, new StringContent(Encrypt.EncryptString(sendingLink, "enigma"), Encoding.UTF8, "application/text"));
         HttpResponseMessage        response     = responseTask.Result;
         string val = response.Content.ReadAsStringAsync().Result;
         if (response.IsSuccessStatusCode)
         {
             HttpCookie myCookie = new HttpCookie("GameCookie")
             {
                 Value = val
             };
             Response.Cookies.Add(myCookie);
             Session["uname"]    = JsonConvert.DeserializeObject <TokenUname>(val).Uname;
             Session["token_id"] = JsonConvert.DeserializeObject <TokenUname>(val).Token_Id;
             Response.Redirect("MainPage.aspx");
         }
         else
         {
             TokenUname value = JsonConvert.DeserializeObject <TokenUname>(val);
             ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('" + value.Token_Id + "')", true);
         }
     }
 }
Esempio n. 2
0
        public HttpResponseMessage GetLoggedIn(string value)
        {
            try
            {
                HttpResponseMessage response;
                MessageWrapper      messageWrapper = JsonConvert.DeserializeObject <MessageWrapper>(Encrypt.DecryptString(value, "enigma"));

                if (new MessageWrapper <TokenUname>().MessageType == messageWrapper.MessageType)
                {
                    TokenUname tokenUname = (TokenUname)messageWrapper.Message;
                    KeyValuePair <int, TokenUname> loggedGamer = LoginFacade.LoginGamer(tokenUname.Token_Id);
                    if (loggedGamer.Key == 200)
                    {
                        response = new HttpResponseMessage(HttpStatusCode.OK)
                        {
                            Content = new StringContent(JsonConvert.SerializeObject(loggedGamer.Value), Encoding.ASCII, "application/json")
                        };
                        return(response);
                    }
                    else
                    {
                        response = new HttpResponseMessage(HttpStatusCode.BadRequest)
                        {
                            Content = new StringContent(JsonConvert.SerializeObject(loggedGamer.Value), Encoding.ASCII, "application/json")
                        };
                        return(response);
                    }
                }
                else
                {
                    Type           messageType    = Type.GetType(messageWrapper.MessageType);
                    var            message        = JsonConvert.DeserializeObject(Convert.ToString(messageWrapper.Message), messageType);
                    LoginGamerInfo loginGamerInfo = (LoginGamerInfo)message;
                    KeyValuePair <int, TokenUname> loggedGamer = LoginFacade.LoginGamer(loginGamerInfo.Uname, loginGamerInfo.Password, loginGamerInfo.RemMe);
                    if (loggedGamer.Key == 200)
                    {
                        response = new HttpResponseMessage(HttpStatusCode.OK)
                        {
                            Content = new StringContent(JsonConvert.SerializeObject(loggedGamer.Value), Encoding.ASCII, "application/json")
                        };
                        return(response);
                    }
                    else
                    {
                        response = new HttpResponseMessage(HttpStatusCode.BadRequest)
                        {
                            Content = new StringContent(JsonConvert.SerializeObject(loggedGamer.Value), Encoding.ASCII, "application/json")
                        };
                        return(response);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }