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);
         }
     }
 }
        private async Task TokenLoginAsync()
        {
            using (var client = new HttpClient())
            {
                HttpCookie myCookie = Request.Cookies["GameCookie"];

                // Read the cookie information and display it.
                if (!IsPostBack && myCookie != null)
                {
                    TokenUname tokenUname = JsonConvert.DeserializeObject <TokenUname>(myCookie.Value.ToString());
                    MessageWrapper <TokenUname> msgInfo = new MessageWrapper <TokenUname>()
                    {
                        Message = tokenUname
                    };
                    HttpResponseMessage responseTask = await client.PostAsync(uri, new StringContent(Encrypt.EncryptString(JsonConvert.SerializeObject(msgInfo), "enigma"), Encoding.UTF8, "application/json"));

                    HttpResponseMessage response = responseTask;
                    if (response.IsSuccessStatusCode)
                    {
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('" + response.IsSuccessStatusCode + "')", true);
                    }
                    else
                    {
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('" + response.IsSuccessStatusCode + "')", true);
                    }
                }
            }
        }
        public void GetLoggedOut(string value)
        {
            try
            {
                MessageWrapper messageWrapper = JsonConvert.DeserializeObject <MessageWrapper>(value);

                if (new MessageWrapper <TokenUname>().MessageType == messageWrapper.MessageType)
                {
                    TokenUname tokenUname = (TokenUname)messageWrapper.Message;
                    LogoutFacade.Logout(tokenUname.Token_Id, tokenUname.Uname);
                }
                else
                {
                    Logger.LogGenerator.GenerateLog(new Logger.Log()
                    {
                        IsException = true, Msg = "Some invalid format attempted login while online", Status_code = 501, Timespan = DateTime.Now.TimeOfDay
                    });
                }
            }
            catch (Exception ex)
            {
                Logger.LogGenerator.GenerateLog(new Logger.Log()
                {
                    IsException = true, Msg = "Weird server error occured.", Status_code = 501, Timespan = DateTime.Now.TimeOfDay
                });
            }
        }
Example #4
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;
            }
        }
 protected void logoutBtn_Click(object sender, EventArgs e)
 {
     using (var client = new HttpClient())
     {
         TokenUname tokenUname = new TokenUname(mainPage.uname, Session["token_id"].ToString());
         MessageWrapper <TokenUname> msgInfo = new MessageWrapper <TokenUname>()
         {
             Message = tokenUname
         };
         string sendingLink = JsonConvert.SerializeObject(msgInfo);
         Task <HttpResponseMessage> responseTask = client.PostAsync(uri, new StringContent(sendingLink, Encoding.UTF8, "application/json"));
         Session.Abandon();
         if (Request.Cookies["GameCookie"] != null)
         {
             Response.Cookies["GameCookie"].Expires = DateTime.Now.AddDays(-1);
         }
         Response.Redirect("LoggedOutPage.aspx");
     }
 }