Esempio n. 1
0
        public HttpCookie CreateFormsAuthenticationCookie(OpenIdUser user)
        {
            Random rand      = new Random();
            int    randomInt = rand.Next(0, int.MaxValue);
            string hashValue = MD5Encryptor.GetHash(randomInt.ToString());

            using (CookiesRepository cookiesRep = new CookiesRepository())
            {
                Cooky existingCookie = cookiesRep.GetList().FirstOrDefault(x => x.UserId == user.UserId);

                if (existingCookie != null)
                {
                    if (cookiesRep.Delete(existingCookie.Id) == false)
                    {
                        return(null);
                    }
                }
                Cooky newCookie = new Cooky()
                {
                    UserId    = user.UserId,
                    HashValue = hashValue
                };

                if (cookiesRep.Create(newCookie) == false)
                {
                    return(null);
                }
            }

            //var ticket = new FormsAuthenticationTicket(1, user.FullName, DateTime.Now, DateTime.Now.AddDays(7), true, user.GetCookieString(hashValue));
            //var encrypted = FormsAuthentication.Encrypt(ticket).ToString();
            var cookie = new HttpCookie(LOGIN_COOKIE_NAME, user.GetCookieString(hashValue));

            return(cookie);
        }
Esempio n. 2
0
        public HttpCookie CreateFormsAuthenticationCookie(OpenIdUser user)
        {
            Random rand = new Random();
            int randomInt = rand.Next(0, int.MaxValue);
            string hashValue = MD5Encryptor.GetHash(randomInt.ToString());

            using (CookiesRepository cookiesRep = new CookiesRepository())
            {
                Cooky existingCookie = cookiesRep.GetList().FirstOrDefault(x => x.UserId == user.UserId);

                if (existingCookie != null)
                {
                    if (cookiesRep.Delete(existingCookie.Id) == false)
                        return null;
                }
                Cooky newCookie = new Cooky()
                {
                    UserId = user.UserId,
                    HashValue = hashValue
                };

                if (cookiesRep.Create(newCookie) == false)
                    return null;
            }

            //var ticket = new FormsAuthenticationTicket(1, user.FullName, DateTime.Now, DateTime.Now.AddDays(7), true, user.GetCookieString(hashValue));
            //var encrypted = FormsAuthentication.Encrypt(ticket).ToString();
            var cookie = new HttpCookie(LOGIN_COOKIE_NAME, user.GetCookieString(hashValue));
            return cookie;
        }