예제 #1
0
        public ClientLogin GetLogin()
        {
            var client = new Client {ClientId = Guid.NewGuid(), SoftwareVersion = "1.0", Type = "iPhone"};

            var hash = StringTools.GetString(System.Security.Cryptography.MD5.Create().ComputeHash(StringTools.GetBytes("pass1")));
            var login = new ClientLogin { Username = "******", Password = hash, ClientInfo = client};

            return login;
        }
예제 #2
0
        public ClientLogin GetLogin()
        {
            var client = new Client {ClientId = Guid.NewGuid(), SoftwareVersion = "1.0", Type = "iPhone"};

            var hash = StringTools.GetMd5Hash("pass1");
            var login = new ClientLogin { Username = "******", Password = hash, ClientInfo = client};

            log.Info(login.Username);

            return login;
        }
예제 #3
0
        // POST api/Login
        public HttpResponseMessage PostLogin(ClientLogin login)
        {
            log.Info(login);

            Login userLogin = null;
            if (ModelState.IsValid)
            {
                try
                {
                    var user = (from u in _db.Users.Include(o => o.Passwords)
                                where u.Username == login.Username &&
                                u.Passwords.OrderByDescending(d => d.Date).FirstOrDefault().PasswordHash == login.Password
                                select u).FirstOrDefault();

                    if (user == null)
                    {
                        return Request.CreateResponse(HttpStatusCode.Unauthorized);
                    }

                    var now = DateTime.Now;
                    userLogin = new Login
                                     {
                                         Ip = ((HttpContextWrapper) Request.Properties["MS_HttpContext"]).Request.UserHostAddress,
                                         User = user,
                                         LoginTime = now,
                                         LogoutTime = now.AddHours(1),
                                         Session = new Session
                                                       {
                                                           Begin = now,
                                                           Expires = now.AddHours(1),
                                                           Token = Guid.NewGuid()
                                                       }
                                     };

                    _db.Logins.Add(userLogin);
                    _db.SaveChanges();
                }
                catch (Exception e)
                {
                    log.Error(e.Message + ": " + e.StackTrace);
                }

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, new {Login = userLogin});
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = userLogin.Id }));
                return response;
            }

            return Request.CreateResponse(HttpStatusCode.BadRequest);
        }