Esempio n. 1
0
            public override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                if (!filterContext.HttpContext.Request.IsAjaxRequest())
                {
                    return;
                }
                filterContext.HttpContext.Response.StatusCode     = 600;
                filterContext.Controller.ViewBag.ValidAuthourized = "0";

                var error      = new StringBuilder();
                var modelState = filterContext.Controller.ViewData.ModelState;

                if (!modelState.IsValid)
                {
                    var errorModel =
                        from x in modelState.Keys
                        where modelState[x].Errors.Count > 0
                        select new
                    {
                        key    = x,
                        errors = modelState[x].Errors.
                                 Select(y => y.ErrorMessage).
                                 ToArray()
                    };

                    foreach (var item in errorModel)
                    {
                        error.AppendLine(string.Format("Error Key: {0} Error Message: {1}", item.key, string.Join(",", item.errors)));
                    }

                    filterContext.HttpContext.Response.AppendHeader("message", error.ToString());
                    return;
                }


                var modelList = filterContext.ActionParameters.Where(ap => ap.Key == "model").ToList();

                if (modelList.IsNullOrEmpty())
                {
                    filterContext.HttpContext.Response.AppendHeader("message", "Invalid Registration Information");
                    return;
                }
                if (!modelList.Any() || modelList.Count != 1)
                {
                    filterContext.HttpContext.Response.AppendHeader("message", "Invalid Registration Information");
                    return;
                }

                var model = modelList[0].Value as PortalUserContract;

                if (model == null)
                {
                    filterContext.HttpContext.Response.AppendHeader("message", "Invalid Registration Information");
                    return;
                }

                if (string.IsNullOrEmpty(model.UserName) || string.IsNullOrEmpty(model.Password) || string.IsNullOrEmpty(model.FirstName) || string.IsNullOrEmpty(model.LastName))
                {
                    filterContext.HttpContext.Response.AppendHeader("message", "Invalid Registration Information");
                    return;
                }


                string msg;
                var    retVal = UserAuthentication.RegisterUser(model, out msg);

                if (!retVal)
                {
                    filterContext.HttpContext.Response.AppendHeader("message", msg.Length > 0 ? msg : "Invalid Registration Information");
                    return;
                }
                filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.OK;
                filterContext.HttpContext.Response.AppendHeader("", "");
                filterContext.Controller.ViewBag.ValidAuthourized = "1";
                base.OnActionExecuting(filterContext);
            }