Пример #1
0
        private void SignIn(RequestLogin customer, bool createPersistentCookie)
        {
            var now = DateTime.UtcNow.ToLocalTime();

            var ticket = new FormsAuthenticationTicket(
                1 /*version*/,
                customer.UserName,
                //_customerSettings.UsernamesEnabled ? customer.Username : customer.Email,
                now,
                now.Add(FormsAuthentication.Timeout),
                createPersistentCookie,
                customer.UserName,
                FormsAuthentication.FormsCookiePath);

            var encryptedTicket = FormsAuthentication.Encrypt(ticket);

            var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
            cookie.HttpOnly = true;
            if (ticket.IsPersistent)
            {
                cookie.Expires = ticket.Expiration;
            }
            cookie.Secure = FormsAuthentication.RequireSSL;
            cookie.Path = FormsAuthentication.FormsCookiePath;
            if (FormsAuthentication.CookieDomain != null)
            {
                cookie.Domain = FormsAuthentication.CookieDomain;
            }
            HttpContext.Response.Cookies.Add(cookie);
        }
Пример #2
0
 public ActionResult Login(RequestLogin req, string ReturnUrl)
 {
     if (ModelState.IsValid)
     {
         if(req.UserName == "admin" && req.Password=="123456")
         {
             SignIn(req, false);
             return RedirectToAction("Index", "Home");
         }
         else
         {
             ModelState.AddModelError("Password", "密码错误");
         }
     }
     return View(req);
 }