コード例 #1
0
 // GET: Account/SignIn
 public ActionResult Login(int? id)
 {
     //Session["RedirectUrl"] = returnUrl;
     var model = new LoginViewModel() { };
     if(id == 1)
     {
         model.userRole = "*User*";
     }
     else
     {
         model.userRole = "*Comp*";
     }
     return View(model);
 }
コード例 #2
0
        public async Task<ActionResult> Login(LoginViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            try
            {
                model.Email = model.Email.Insert(0,model.userRole);
                //Find if there is a redirect Url. Then remove it for next time!
                //var redirectUrl = Session["RedirectUrl"] as string;
                //Session["RedirectUrl"] = null;
                var result = await WebApiService.Instance.AuthenticateAsync<SignInResult>(model.Email, model.Password);
                var userInfo = await WebApiService.Instance.GetAsync<UserModel>("/api/Account/me", result.AccessToken);
                userInfo.Token = result.AccessToken;
                string json = JsonConvert.SerializeObject(userInfo);

                FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, model.Email, DateTime.Now, DateTime.Now.AddMinutes(20), model.RememberMe, json, "/");
                HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(authTicket));
                Response.Cookies.Add(cookie);

                //return Redirect(redirectUrl ?? "/");
                return RedirectToAction("Index", "Home");
            }
            catch (ApiException ex)
            {
                //No 200 OK result, what went wrong?
                HandleBadRequest(ex);

                if (!ModelState.IsValid)
                {
                    return View(model);
                }

                throw;
            }
        }