コード例 #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
ファイル: OpenIdService.cs プロジェクト: boujnah5207/gadev
        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
ファイル: UsersController.cs プロジェクト: boujnah5207/gadev
        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);
            }
        }