Пример #1
0
    public void Handle(HttpListenerRequest requestInfo, HttpListenerResponse responseInfo)
    {
        var sessionInterceptor = new SessionInterceptor();
        var userId             = sessionInterceptor.GetCurrentUserBySessionId(requestInfo, responseInfo);

        if (userId != null)
        {
            var user = InMemoryDb.Users.First(w => w.Id == userId);
            responseInfo.StatusCode = 200;
            var balance = new
            {
                balance = user.Balance
            };
            var json  = JsonConvert.SerializeObject(balance);
            var bytes = Encoding.UTF8.GetBytes(json);

            responseInfo.OutputStream.Write(bytes);
        }
        else
        {
            responseInfo.StatusCode = 401;
            responseInfo.StatusCode = 401;
            var errorMessage = "Unauthorized!";
            var json         = JsonConvert.SerializeObject(errorMessage);
            var bytes        = Encoding.UTF8.GetBytes(json);

            responseInfo.OutputStream.Write(bytes);
        }
    }
Пример #2
0
    static void Main(string[] args)
    {
        //using (var httpClient = new HttpClient())
        //{
        //    var uri = "http://*****:*****@"c:\data\welcome-page.html");
                var echoContent  = Encoding.UTF8.GetBytes(responsePage);
                responseInfo.AddHeader("Content-Type", "text/html");
                responseInfo.OutputStream.Write(echoContent, 0, echoContent.Length);
                responseInfo.Close();

                continue;
            }
            else if (requestInfo.RawUrl == "/app/get-me")
            {
                var sessionInterceptor = new SessionInterceptor();
                var userId             = sessionInterceptor.GetCurrentUserBySessionId(requestInfo, responseInfo);
            }
            else if (requestInfo.RawUrl == "/app/change-password")
            {
                var handler = Router.Routes[requestInfo.RawUrl];
                responseInfo.AddHeader("Content-Type", "application/json");
                handler.Handle(requestInfo, responseInfo);

                responseInfo.Close();
            }
            else
            {
                var handler = Router.Routes[requestInfo.RawUrl];

                responseInfo.AddHeader("Content-Type", "application/json");
                handler.Handle(requestInfo, responseInfo);

                responseInfo.Close();
            }
        }

        Console.WriteLine("Completed request processing!");
    }
Пример #3
0
    public void Handle(HttpListenerRequest requestInfo, HttpListenerResponse responseInfo)
    {
        var sessionInterceptor = new SessionInterceptor();
        var userId             = sessionInterceptor.GetCurrentUserBySessionId(requestInfo, responseInfo);

        if (userId != null)
        {
            using (var ms = new MemoryStream())
            {
                requestInfo.InputStream.CopyTo(ms);
                var requestBody   = ms.ToArray();
                var requestString = Encoding.UTF8.GetString(requestBody);

                var command = JsonConvert.DeserializeObject <ChangePasswordCommand>(requestString);

                var userPasswordChange = InMemoryDb.Users.FirstOrDefault(w => w.Email == "*****@*****.**") as ApplicationUser;
                userPasswordChange.Password = command.NewPassword;
                responseInfo.StatusCode     = 200;
            }
        }
        else
        {
            responseInfo.StatusCode = 401;
            var errorMessage = "Unauthorized!";
            var json         = JsonConvert.SerializeObject(errorMessage);
            var bytes        = Encoding.UTF8.GetBytes(json);

            responseInfo.OutputStream.Write(bytes);
        }
    }