Exemple #1
0
        public ActionResult AjaxRegister(RegisterModel model)
        {
            if (!Util.IsAjaxRequest(Request))
            {
                Util.ThrowBadRequestException("Not ajax post.");
            }

            // bool is_lau = model.UserName[0] == '_';
            bool is_lau = SandId.IsLau(model.UserName);

            if (!is_lau)
            {
                object ret_obj = Warehouse.RateLimiter.Validate(CarryType.Register);
                if (ret_obj != null)
                {
                    return(Json(ret_obj));
                }
            }

            string check_name = is_lau ? model.UserName.Substring(1) : model.UserName;

            if (!Util.WithinCharSetUserName(check_name))
            {
                Util.ThrowBadRequestException("使用者名稱含有不合法的字元。");
            }

            if (ModelState.IsValid)
            {
                try
                {
                    WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
                    WebSecurity.Login(model.UserName, model.Password);

                    int user_id = WebSecurity.GetUserId(model.UserName);

                    onLogin(model.UserName, model.m_id, user_id);
                    Trace.TraceInformation("註冊成功。UserName={0}, m_id={1}.", model.UserName, model.m_id);
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));

                    Trace.TraceWarning("註冊失敗。UserName={0}, m_id={1}.", model.UserName, model.m_id);

                    // Util.ThrowBadRequestException("Ajax register failed. " + Util.ValidationSummary(ModelState));
                    return(new ErrorResult("Ajax register failed. " + Util.ValidationSummary(ModelState)));
                }
            }
            else
            {
                Util.ThrowBadRequestException("Ajax register failed. " + Util.ValidationSummary(ModelState));
            }

            return(Json(new { ok = true }));
        }
Exemple #2
0
        public ActionResult AjaxLogin(LoginModel model)
        {
            if (!Util.IsAjaxRequest(Request))
            {
                Util.ThrowBadRequestException("Not ajax post.");
            }

            // bool is_lau = model.UserName[0] == '_';
            bool is_lau = SandId.IsLau(model.UserName);

            if (!is_lau)
            {
                object ret_obj = Warehouse.RateLimiter.Validate(CarryType.Login);
                if (ret_obj != null)
                {
                    return(Json(ret_obj));
                }
            }

            if (!ModelState.IsValid)
            {
                Trace.TraceWarning("登入失敗。UserName={0}, m_id={1}.", model.UserName, model.m_id);
                Util.ThrowBadRequestException("Ajax login failed. " + Util.ValidationSummary(ModelState));
            }
            else if (WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
            {
                int user_id = WebSecurity.GetUserId(model.UserName);

                onLogin(model.UserName, model.m_id, user_id);
                Trace.TraceInformation("登入成功。UserName={0}, m_id={1}.", model.UserName, model.m_id);
            }
            else
            {
                Trace.TraceWarning("登入失敗。UserName={0}, m_id={1}.", model.UserName, model.m_id);
                Util.ThrowBadRequestException("所提供的使用者名稱或密碼不正確。");
            }

            return(Json(new { ok = true }));
        }