public async Task <HandlerResponse> Handle(SignInViewModel model)
        {
            var user = await _dbContext.Users.Include(p => p.Role).FirstOrDefaultAsync(p => p.PhoneNumber == model.PhoneNumber && p.Password == model.Password);

            if (user is null)
            {
                return new HandlerResponse()
                       {
                           StatusCode  = 404,
                           Discription = "Not Found",
                           Route       = new KeyValuePair <string, string>("Accoun", "SignIn")
                       }
            }
            ;

            var sessionId = _sessionManagementService.AddSession(user.Id);

            return(new HandlerResponse()
            {
                StatusCode = 200,
                SessionId = sessionId,
                UserId = user.Id,
                UserName = user.Name,
                Discription = "Ok",
                Route = new KeyValuePair <string, string>("Account", "MyCabinet")
            });
        }
    }
Пример #2
0
        public void Handle(HttpListenerRequest request, HttpListenerResponse response)
        {
            using (var ms = new MemoryStream())
            {
                request.InputStream.CopyTo(ms);
                var content           = Encoding.UTF8.GetString(ms.ToArray());
                var registrationModel = JObject.Parse(content);

                var phoneNumber = registrationModel["phoneNumber"].ToString();
                var password    = registrationModel["password"].ToString();

                var user = _dbContext.ApplicationUsers
                           .SingleOrDefault(p => p.PhoneNumber == phoneNumber && p.PasswordHash == password);

                if (user == null)
                {
                    response.StatusCode = 404;

                    response.OutputStream.Write(Encoding.UTF8.GetBytes("NOT FOUND!"));
                    response.Close();
                    return;
                }

                var session = _sessionManagementService.AddSession(user.Id);
                response.StatusCode = 200;

                response.OutputStream.Write(Encoding.UTF8.GetBytes(session));
                response.Close();
            }
        }
Пример #3
0
        public void Handle(HttpListenerRequest request, HttpListenerResponse response)
        {
            using (var ms = new MemoryStream())
            {
                if (request.HttpMethod == "OPTIONS")
                {
                    response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With");
                    response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
                    response.AddHeader("Access-Control-Max-Age", "1728000");
                }
                response.AppendHeader("Access-Control-Allow-Origin", "*");

                request.InputStream.CopyTo(ms);
                var content           = Encoding.UTF8.GetString(ms.ToArray());
                var registrationModel = JObject.Parse(content);

                var phoneNumber = registrationModel["phoneNumber"].ToString();
                var password    = registrationModel["password"].ToString();

                var user = _dbContext.ApplicationUsers
                           .SingleOrDefault(p => p.PhoneNumber == phoneNumber && p.PasswordHash == password);

                if (user == null)
                {
                    response.StatusCode = 404;

                    response.OutputStream.Write(Encoding.UTF8.GetBytes("NOT FOUND!"));
                    response.Close();
                    return;
                }

                var session = _sessionManagementService.AddSession(user.Id);

                shortUser su   = new shortUser(user.Id, session, user.PhoneNumber);
                var       json = JsonConvert.SerializeObject(su);

                response.StatusCode = 200;

                response.OutputStream.Write(Encoding.UTF8.GetBytes(json));
                response.Close();
            }
        }