예제 #1
0
        public IHttpActionResult Postsuser(suser suser)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (suserExists(suser.UID))
            {
                return(new PageResult("Conflict", Request));
            }
            suser.Pass       = ValidCodeUtils.EncryptPassword(suser.Pass);
            suser.CreateDate = DateTime.Now;
            db.suser.Add(suser);

            try
            {
                db.SaveChanges();
            }
            catch (Exception)
            {
                throw;
            }

            return(Content <string>(HttpStatusCode.OK, "OK"));
        }
예제 #2
0
        public IHttpActionResult Putsuser(int id, suser suser)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != suser.UserId)
            {
                return(BadRequest());
            }
            suser.UpdateDate = DateTime.Now;

            db.Entry(suser).State = EntityState.Modified;

            if (suser.Pass == null || suser.Pass.Trim() == "")
            {
                db.Entry(suser).Property("Pass").IsModified = false;
            }
            else
            {
                suser.Pass = ValidCodeUtils.EncryptPassword(suser.Pass);
            }

            try
            {
                db.SaveChanges();
            }
            catch (Exception)
            {
                if (suserExists(suser.UID))
                {
                    return(new PageResult("Conflict", Request));
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
예제 #3
0
 public IHttpActionResult ValidatePass(int uid, string password)
 {
     try
     {
         var result = new UserDataPackage <User>();
         password = ValidCodeUtils.EncryptPassword(password);
         if (!ValidateUser(uid, password))
         {
             return(new PageResult("error", Request));
         }
         result = new UserDataPackage <User> {
             code = 20000
         };
         return(new PageResult(result, Request));
     }
     catch (Exception)
     {
         throw;
     }
 }
예제 #4
0
        public IHttpActionResult Login()
        {
            try
            {
                var             result  = new UserDataPackage <User>();
                HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"];
                HttpRequestBase request = context.Request;

                string str = new System.IO.StreamReader(request.InputStream).ReadToEnd();

                User userInfo = JsonConvert.DeserializeObject <User>(str);

                userInfo.password = ValidCodeUtils.EncryptPassword(userInfo.password);

                if (!ValidateUser(userInfo.uid, userInfo.password))
                {
                    result = new UserDataPackage <User> {
                        code = 20001
                    };
                    return(new PageResult(result, Request));
                }

                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(0, userInfo.uid, DateTime.Now,
                                                                                 DateTime.Now.AddHours(1), true, string.Format("{0}&{1}", userInfo.uid, userInfo.password),
                                                                                 FormsAuthentication.FormsCookiePath);
                string authTicket = FormsAuthentication.Encrypt(ticket);

                result = new UserDataPackage <User> {
                    data = new User {
                        token = authTicket
                    }, code = 20000
                };

                return(new PageResult(result, Request));
            }
            catch (Exception ex)
            {
                return(new PageResult(ex.ToString(), Request));
            }
        }