Esempio n. 1
0
        public async Task <CookieLoginViewModel> RetrieveLoginCookie()
        {
            var cookieLoginVM = new CookieLoginViewModel();
            // first, let's see if the cookie even exists
            var cookie = HttpContext.Current.Request.Cookies[$"{cookiePrefix}login"];

            if (cookie != null)
            {
                // let's make sure it's not expired
                var dateDiff = new TimeSpan();
                dateDiff = cookie.Expires - DateTime.Now;
                if (dateDiff.Days > 0)
                {
                    var id   = cookie["ID"];
                    var code = cookie["Code"];
                    if (!string.IsNullOrEmpty(id))
                    {
                        if (!string.IsNullOrEmpty(code))
                        {
                            // we have everything that we need to continue
                            var cookieDB = await _service.RetrieveCookieFromDatabase(int.Parse(id), code, _context);

                            if (cookieDB.ID > 0)
                            {
                                cookieLoginVM = script.Deserialize <CookieLoginViewModel>(cookieDB.Data);
                            }
                        }
                    }
                }
            }
            return(cookieLoginVM);
        }
        public async Task <ActionResult> Login(CookieLoginViewModel model)
        {
            var user = await this.userManager.AuthenticateUserWithPasswordAsync(model.Username, model.Password);

            if (user.Identity.IsAuthenticated)
            {
                var cookieIdentity = new SentinelIdentity(DefaultAuthenticationTypes.ApplicationCookie, user.Identity);

                this.Authentication.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
                this.Authentication.SignIn(
                    new AuthenticationProperties
                {
                    IsPersistent = true,
                    ExpiresUtc   = DateTimeOffset.UtcNow.AddHours(1),
                    RedirectUri  = model.ReturnUrl
                },
                    cookieIdentity.ToClaimsIdentity());

                if (!string.IsNullOrEmpty(model.ReturnUrl) && Url.IsLocalUrl(model.ReturnUrl))
                {
                    return(this.Redirect(model.ReturnUrl));
                }
            }

            return(this.View(model));
        }
Esempio n. 3
0
        public async Task <HttpCookie> CreateLoginCookieAsync(int userID, string token, bool rememberMe)
        {
            var cookieViewModel = new CookieLoginViewModel {
                userID     = userID,
                Token      = token,
                RememberMe = rememberMe
            };
            var cookie     = new HttpCookie("null");
            var cookieType = await _context.CookieTypes.FirstOrDefaultAsync(x => x.Index == 100);

            var data       = script.Serialize(cookieViewModel);
            var expiration = rememberMe ?
                             DateTimeOffset.Now.AddDays(14) :
                             DateTimeOffset.Now.AddDays(1);
            var pullModel = await _service.CreateCookieDataInstanceAsync(data, expiration, cookieType, _context);

            if (pullModel.ID > 0)
            {
                cookie = CreateCookie("login", expiration, pullModel);
            }
            return(cookie);
        }