コード例 #1
0
        public async Task <ActionResult> SignOut()
        {
            cookie = Request.Cookies["ForumCookie"];
            if (cookie != null)
            {
                var UserId    = Convert.ToInt64(cookie["UserId"]);
                var SecretKey = cookie["SecretKey"];
                var skey      = Crypto.Hash(SecretKey, IPaddress.GetIP());

                var c = db.Cookies.Where(p => p.UserId == UserId && p.SecretKey == skey);
                if (await c.AnyAsync())
                {
                    db.Cookies.RemoveRange(c);
                    await db.SaveChangesAsync();
                }

                cookie.Expires = DateTime.Now.AddDays(-1);
                Response.Cookies.Add(cookie);
            }

            if (TempData["User"] != null)
            {
                TempData.Remove("User");
            }
            return(RedirectToAction("Index", "Home"));
        }
コード例 #2
0
        public async Task <ActionResult> Authorization(AuthorizationViewModel AuthorizationModel)
        {
            if (await db.Users.Where(p => p.Login == AuthorizationModel.Login).AnyAsync())
            {
                var user = await db.Users.FirstOrDefaultAsync(p => p.Login == AuthorizationModel.Login);

                var password = Crypto.Hash(AuthorizationModel.Password, user.Salt);

                if (user.Login == AuthorizationModel.Login && Convert.ToBase64String(user.Password) == Convert.ToBase64String(password))
                {
                    if (AuthorizationModel.RememberMe == true)
                    {
                        var IPv4      = IPaddress.GetIP();
                        var secretKey = System.Guid.NewGuid().ToString();

                        cookie              = new HttpCookie("ForumCookie");
                        cookie["UserId"]    = user.Id.ToString();
                        cookie["SecretKey"] = secretKey;
                        cookie.Expires      = DateTime.Now.AddDays(30);

                        Response.Cookies.Add(cookie);

                        db.SetCookie(user.Nickname, IPv4, Crypto.Hash(secretKey, ""), DateTime.Now.AddDays(30));
                    }
                    else
                    {
                        HttpCookie cookie = new HttpCookie("ForumCookie");
                        cookie["UserId"]   = user.Id.ToString();
                        cookie["Password"] = Convert.ToBase64String(user.Password);
                        cookie.Expires     = DateTime.Now.AddMinutes(10);

                        Response.Cookies.Add(cookie);
                    }
                }
                else
                {
                    TempData["Alert"] = "Wrong login or password!!!";
                    return(RedirectToAction("Index", "Home"));
                }
            }
            else
            {
                TempData["Alert"] = "Wrong login or password!!!";
                return(RedirectToAction("Index", "Home"));
            }
            return(RedirectToAction("Index", "Home"));
        }