Esempio n. 1
0
        public async Task<ActionResult> Login(AccountViewModel model, string returnUrl = "/Admin/Home/Index") {
            if (this.ModelState.IsValid) {
                string data = string.Format("grant_type=password&username={0}&password={1}", model.UserName, model.Password);
                var response = await new iTalkClient().PostAsync("/Token", new StringContent(data));

                if (response.StatusCode == HttpStatusCode.OK) {
                    string authCookie = response.Headers.GetValues("Set-Cookie").First();
                    string[] parts = authCookie.Split(';')[0].Split('=');
                    this.Response.AppendCookie(new HttpCookie(parts[0], parts[1]));
                    return this.RedirectToAction("Index", "Home");
                }

                var json = await response.Content.ReadAsStringAsync();
                try {
                    dynamic result = JValue.Parse(json);
                    this.ModelState.AddModelError(string.Empty, (string)result.error_description);
                }
                catch (Exception) {
                    this.ModelState.AddModelError(string.Empty, json);
                }
            }

            this.ViewBag.ReturnUrl = returnUrl;
            this.ViewBag.Title = Resources.Login;

            return this.View(model);
        }
Esempio n. 2
0
        public async Task<ExecuteResult> Post() {
            AccountViewModel model = new AccountViewModel();
            model.Alias = HttpContext.Current.Request.Form["Alias"];
            model.Password = HttpContext.Current.Request.Form["Password"];
            model.PersonalSign = HttpContext.Current.Request.Form["PersonalSign"];
            model.UserName = HttpContext.Current.Request.Form["UserName"];

            this.CheckModelState(model);

            iTalkUser user = new iTalkUser() {
                UserName = model.UserName,
                Alias = model.Alias,
                PersonalSign = model.PersonalSign
            };

            HttpPostedFile file = this.CheckPortrait();

            if (file != null) {
                Portrait portrait = this.CreatePortrait(file);
                user.Portrait = portrait;
            }

            IdentityResult result;

            try {
                result = await this.UserManager.CreateAsync(user, model.Password);
            }
            catch (Exception ex) {
                throw this.CreateResponseException(HttpStatusCode.BadRequest, ex.Message);
            }

            if (!result.Succeeded) {
                throw this.CreateResponseException(HttpStatusCode.BadRequest, string.Join(",", result.Errors));
            }

            // for ActionFilterAttribute
            this.Request.Properties.Add("UserName", model.UserName);

            return new ExecuteResult(true);
        }