Exemplo n.º 1
0
        public ActionResult SetRandenPassword(string Token = null)
        {
            if (string.IsNullOrWhiteSpace(Token))
            {
                return(RedirectToAction("Index"));
            }
            var env = new Env();
            var jws = new SimpleJws();

            if (jws.Validate(Token, env.SecretKey))
            {
                var payload  = jws.Decode(Token);
                var MemberID = (string)payload["MemberID"];
                var data     = db.Members.Where(m => m.MemberID == MemberID).FirstOrDefault();
                var password = ChoyPassword.Hash((string)payload["Password"], TimeConverter.ToTimestamp(data.CreateAt));
                if (data.Psw != password)
                {
                    try
                    {
                        data.Psw             = password;
                        db.Entry(data).State = EntityState.Modified;
                        db.SaveChanges();
                    }
                    catch
                    {
                        ViewBag.Error = "Sorry, the server is busy. Please try again later.";
                        return(View());
                    }
                }
            }
            else
            {
                ViewBag.Error = "The apply has expired";
                return(View());
            }

            var url = Url.Action("Index") + "#/login";

            return(Redirect(url));
        }
Exemplo n.º 2
0
        public HttpResponseMessage Register(ApiAuthRegister data)
        {
            Env                 env       = new Env();
            SimpleJws           jws       = new SimpleJws();
            HttpResponseMessage response  = new HttpResponseMessage();
            string              token     = data.Token;
            bool                isSuccess = true;
            string              messages  = "";

            if (token == null) // 缺乏 token
            {
                isSuccess           = false;
                messages            = "You don't have permission to access this server.";
                response.StatusCode = HttpStatusCode.Unauthorized; // 401
            }
            else if (!jws.Validate(token, env.SecretKey))          // 缺乏 token
            {
                isSuccess           = false;
                messages            = "註冊連結已失效!!!";
                response.StatusCode = HttpStatusCode.Unauthorized; // 401
            }
            else if (data.Password.Length < 6 || data.Password.Length > 18)
            {
                isSuccess           = false;
                messages            = "Your password must be between 6 and 18 characters";
                response.StatusCode = HttpStatusCode.BadRequest; // 400
            }
            else
            {
                MemberSystem memberSystem       = new MemberSystem();
                Dictionary <string, object> jwt = jws.Decode(token);
                if (!jwt.ContainsKey("Email") || !jwt.ContainsKey("Gender") || !jwt.ContainsKey("Birthday"))
                {
                    isSuccess           = false;
                    messages            = "Invalid operation.";
                    response.StatusCode = HttpStatusCode.BadRequest; // 400
                }
                else
                {
                    string name = jwt.ContainsKey("UserName")
            ? (string)jwt["UserName"]
            : null;
                    DateTime now    = DateTime.Now;
                    Member   member = new Member
                    {
                        Email         = (string)jwt["Email"],
                        Psw           = ChoyPassword.Hash(data.Password, TimeConverter.ToTimestamp(now)),
                        NickName      = name,
                        Gender        = (bool)jwt["Gender"],
                        Bday          = TimeConverter.ToDateTime((long)jwt["Birthday"]),
                        ContactEmail  = (string)jwt["Email"],
                        CreateAt      = now,
                        ProfilePic    = memberSystem.GetFileBytes("\\Images\\carot.png"),
                        ImageMimeType = "image/png",
                        PerCode       = 0,
                        IsSuspended   = false,
                        LastLogInTime = now
                    };
                    if (!memberSystem.Register(member))
                    {
                        isSuccess           = false;
                        messages            = "Registration failed";
                        response.StatusCode = HttpStatusCode.InternalServerError; // 500
                    }
                    else
                    {
                        isSuccess           = true;
                        messages            = "Registration success";
                        response.StatusCode = HttpStatusCode.OK; // 200
                    }
                }
            }

            var result = new
            {
                Success  = isSuccess,
                Messages = messages
            };

            response.Content = new StringContent(JsonConvert.SerializeObject(result));
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            return(response);
        }