コード例 #1
0
        private bool setSession(vmLogin model)
        {
            try
            {
                this.isModelValid();

                model = model.getData();

                dynamic data = null;
                var     sc   = new StringContent(JsonConvert.SerializeObject(new { username = model.username, password = model.password }), Encoding.UTF8, "application/json");

                using (HttpClient hc = new HttpClient())
                {
                    hc.BaseAddress = new Uri(Config.getValue("webapi"));
                    HttpResponseMessage hrm = hc.PostAsync("api/login", sc).Result;

                    if (hrm.IsSuccessStatusCode)
                    {
                        data = JObject.Parse(hrm.Content.ReadAsStringAsync().Result);
                    }
                    else
                    {
                        throw new Exception("Error Code: " + hrm.StatusCode + "|Message: " + hrm.ReasonPhrase);
                    }
                }

                ApplicationUser appUser = new ApplicationUser()
                {
                    Id       = Assess.setString((string)data.user.id).ToLower(),
                    UserName = Assess.setString((string)data.user.name),
                    Token    = Assess.setString((string)data.token),
                };

                ApplicationSignInManager signInManager = this.HttpContext.GetOwinContext().Get <ApplicationSignInManager>();
                signInManager.SignIn(appUser, isPersistent: model.rememberme, rememberBrowser: false);

                return(true);
            }
            catch (Exception ex)
            {
                this.addModelError(ex);
            }

            return(false);
        }