Пример #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);
        }
Пример #2
0
        public ActionResult DeleteConfirmed(int id)
        {
            if (Authorized(RoleType.SystemManager))
            {
                User user;
                using (CookiesRepository cookieRep = new CookiesRepository())
                    using (UsersRepository userRep = new UsersRepository(CurrentUser.CompanyId))
                    {
                        user = userRep.GetEntity(id);

                        if (user == null)
                        {
                            return(Error(Loc.Dic.error_user_not_found));
                        }
                        if (user.Id == CurrentUser.UserId)
                        {
                            return(Error(Loc.Dic.error_user_cannot_delete_self));
                        }
                        if (user.CompanyId != CurrentUser.CompanyId || user.Roles == (int)RoleType.SuperAdmin)
                        {
                            return(Error(Loc.Dic.error_no_permission));
                        }

                        user.IsActive = false;
                        userRep.Update(user);

                        Cooky expiredCookie = cookieRep.GetList().SingleOrDefault(x => x.UserId == user.Id);
                        if (expiredCookie != null)
                        {
                            cookieRep.Delete(expiredCookie.Id);
                        }
                    }

                return(RedirectToAction("Index"));
            }
            else
            {
                return(Error(Loc.Dic.error_no_permission));
            }
        }
Пример #3
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;
        }
Пример #4
0
        public ActionResult DeleteConfirmed(int id)
        {
            if (Authorized(RoleType.SystemManager))
            {
                User user;
                using (CookiesRepository cookieRep = new CookiesRepository())
                using (UsersRepository userRep = new UsersRepository(CurrentUser.CompanyId))
                {
                    user = userRep.GetEntity(id);

                    if (user == null)
                    {
                        return Error(Loc.Dic.error_user_not_found);
                    }
                    if (user.Id == CurrentUser.UserId)
                    {
                        return Error(Loc.Dic.error_user_cannot_delete_self);
                    }
                    if (user.CompanyId != CurrentUser.CompanyId || user.Roles == (int)RoleType.SuperAdmin)
                    {
                        return Error(Loc.Dic.error_no_permission);
                    }

                    user.IsActive = false;
                    userRep.Update(user);

                    Cooky expiredCookie = cookieRep.GetList().SingleOrDefault(x => x.UserId == user.Id);
                    if (expiredCookie != null)
                    {
                        cookieRep.Delete(expiredCookie.Id);
                    }
                }

                return RedirectToAction("Index");
            }
            else
            {
                return Error(Loc.Dic.error_no_permission);
            }
        }