Пример #1
0
        public ActionResult Login(LoginViewModel model, string returnUrl)
        {
            if(ModelState.IsValid)
            {
                var user = UserManager.UserLogin(model.Login, model.Password, HttpContext.Request.UserHostAddress);
                if(user!=null)
                {
                    var authTicket = new FormsAuthenticationTicket(
                    10, // Version
                    user.Login, // User Login
                    DateTime.Now, // Issue-Date
                    DateTime.Now.AddMinutes(FormsAuthentication.Timeout.TotalMinutes), // Expiration
                    model.RememberMe, //TODO: Remember me
                    user.Role, // User Role
                    FormsAuthentication.FormsCookiePath // Cookie Path
                    );
                    var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(authTicket));
                    if (authTicket.IsPersistent)
                        authCookie.Expires = authTicket.Expiration;
                    Response.Cookies.Add(authCookie);
                    return RedirectToAction("Index", "Profile",new {userId=user.Id});
                }

            }
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
            return View(model);
        }
Пример #2
0
        public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            // This doesn't count login failures towards account lockout
            // To enable password failures to trigger account lockout, change to shouldLockout: true
            string userName = model.Login;
            if (UserManager.FindByName(userName) == null && UserManager.FindByEmail(model.Login) != null)
                userName = UserManager.FindByEmail(model.Login).Email;
            var result = await SignInManager.PasswordSignInAsync(userName, model.Password, 
                model.RememberMe, shouldLockout: true);
            switch (result)
            {
                case SignInStatus.Success:
                    return RedirectToLocal(returnUrl);
                case SignInStatus.LockedOut:
                    return View("Lockout");
                case SignInStatus.RequiresVerification:
                    return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, 
                        RememberMe = model.RememberMe });
                case SignInStatus.Failure:
                default:
                    ModelState.AddModelError("", "Invalid login attempt.");
                    return View(model);
            }
        }
Пример #3
0
        public async Task<ActionResult> Login(LoginViewModel model)
        {
            SuccessLoginResponse loginData = new SuccessLoginResponse();
            using (var client = new HttpClient())
            {
                var values = new Dictionary<string, string>
                {
                    { "UserName", model.UserName },
                    { "Password", model.Password },
                    { "grant_type", "password"}
                };

                var content = new FormUrlEncodedContent(values);
                var response =await client.PostAsync ("http://localhost:54486/Token", content);

                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    var responseString = await response.Content.ReadAsStringAsync();
                    //var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                    //var jsonObject = serializer.DeserializeObject(responseString);

                    loginData = JsonConvert.DeserializeObject<SuccessLoginResponse>(responseString);

                    FormsAuthentication.Initialize();
                    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
                        loginData.UserName,
                        DateTime.Now,
                        DateTime.Now.AddMinutes(30), // value of time out property
                        false, // Value of IsPersistent property
                        loginData.AccessToken,
                        FormsAuthentication.FormsCookiePath);

                    string encryptedTicket = FormsAuthentication.Encrypt(ticket);
                    HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                    if (ticket.IsPersistent)
                    {
                        cookie.Expires = ticket.Expiration;
                    }
                    Response.Cookies.Add(cookie);
                    Session["UserInfo"] = loginData;
                }
            }
                    return Json(loginData);
        }