Ejemplo n.º 1
0
        public static Data.User ControlLogin()
        {
            if (HttpContext.Current.Session["User"] != null)
            {
                return((Data.User)HttpContext.Current.Session["User"]);
            }



            if (HttpContext.Current.Request.Cookies["userauth"] != null)
            {
                string tokenFromCookie = HttpContext.Current.Request.Cookies["userauth"].Value;

                using (Data.SimpleData db = new Data.SimpleData())
                {
                    Data.Token token = db.Tokens.FirstOrDefault(t => t.TokenKey == tokenFromCookie && t.ExpireDate > DateTime.Now);

                    if (token != null)
                    {
                        HttpContext.Current.Session["User"] = token.User;
                        return(token.User);
                    }
                }
            }

            return(null);
        }
        public ActionResult Login(Data.User user)
        {
            string returnUrl = Request["returnUrl"];

            LoginResponse response = new LoginResponse((int)CommonContant.LANGUAGEID.TR);

            try
            {
                using (Data.SimpleData db = new Data.SimpleData())
                {
                    string password = Security.sha512encrypt(user.Password).Substring(0, 70);
                    var    User     = db.Users.FirstOrDefault(t => t.Email == user.Email && t.Password == password);


                    if (User == null)
                    {
                        response.SetErrror(CommonContant.ERROR_CODE.NONACTIVEUSER);
                        return(View(response));
                    }

                    if (User.ActiveStatus != (int)CommonContant.ActiveStatus.activeuser)
                    {
                        response.SetErrror(CommonContant.ERROR_CODE.NONACTIVEUSER);
                        return(View(response));
                    }


                    Data.Token token = new Data.Token
                    {
                        CreateDate = DateTime.Now,
                        ExpireDate = DateTime.Now.AddHours(6),
                        TokenKey   = Security.sha512encrypt(RandomSfr.Generate(20)),
                    };
                    User.Tokens.Add(token);
                    db.SaveChanges();

                    HttpCookie c*k = new HttpCookie("userauth", token.TokenKey);
                    c*k.Expires = DateTime.Now.AddHours(6);
                    Response.Cookies.Add(c*k);

                    Session["User"] = User;

                    if (string.IsNullOrEmpty(returnUrl))
                    {
                        return(RedirectToAction("Index", "Home"));
                    }
                    else
                    {
                        return(Redirect(returnUrl));
                    }
                }
            }
            catch (Exception ex)
            {
                response.SetErrror(CommonContant.ERROR_CODE.SYSTEM_ERROR);
            }

            return(View());
        }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //  eğer sesion kullanırsak
            if (filterContext.RequestContext.HttpContext.Session["User"] == null)
            {
                filterContext.Result = new  RedirectResult("/Account/Login", false);

                return;
            }


            //todo User içinde roller hatalı bu nedenle tekrar db nesnesini kullandık incele   @KADİR
            var User = Service.LoginControl.ControlLogin();

            if (User == null)
            {
                filterContext.Result = new RedirectResult("/Account/Login", false);
                return;
            }



            if (!string.IsNullOrEmpty(RoleNames))
            {
                Data.SimpleData  db          = new Data.SimpleData();
                List <Data.Role> userDbRoles = db.Users.FirstOrDefault(t => t.ID == User.ID).Roles.ToList();


                string[]      roles     = RoleNames.Split(';');
                List <string> userroles = userDbRoles.Select(t => t.Name).ToList();
                var           result    = true;
                foreach (var item in roles)
                {
                    if (!userroles.Contains(item))
                    {
                        result = false;
                        break;
                    }
                }

                if (result != true)
                {
                    //yetkisiz bir sayfaya giriş yapılmaya çalışılıyor
                    filterContext.Result = new RedirectResult("/Error/Index", false);
                }
            }
        }
        // GET: Activation
        public ActionResult Activate(string email, string validationkey)
        {
            Responses.BaseResponse response = new Responses.BaseResponse((int)CommonContant.LANGUAGEID.TR);

            try
            {
                using (Data.SimpleData db = new Data.SimpleData())
                {
                    var    User    = db.Users.FirstOrDefault(t => t.Email == email);
                    string userkey = null;

                    if (User != null)
                    {
                        userkey = Security.sha512encrypt(User.ValidationKey);
                    }

                    if (userkey == validationkey)
                    {
                        User.ActiveStatus    = (int)CommonContant.ActiveStatus.activeuser;
                        User.ValidationKey   = RandomSfr.Generate(10);
                        db.Entry(User).State = System.Data.Entity.EntityState.Modified;
                        db.SaveChanges();
                    }
                    else
                    {
                        response.SetErrror(CommonContant.ERROR_CODE.SECURTYERROR);
                        return(View(response));
                    }
                }
            }
            catch (Exception ex)
            {
                response.SetErrror(CommonContant.ERROR_CODE.SYSTEM_ERROR);
                return(View(response));
            }


            return(View(response));
        }
        public async Task <ActionResult> Register(Data.User user)
        {
            user.EmailConfirm  = false;
            user.ActiveStatus  = (int)CommonContant.ActiveStatus.pasif;
            user.Password      = SimpleLogin.Common.Security.sha512encrypt(user.Password).Substring(0, 70);
            user.ValidationKey = RandomSfr.Generate(10);
            Data.SimpleData db = new Data.SimpleData();
            db.Entry(user).State = System.Data.Entity.EntityState.Added;

            try
            {
                int result = await db.SaveChangesAsync();

                //db ye kayıt edildi
                if (result == 1)
                {
                    string link = "http://localhost:58522/Activation/Activate/" + user.Email + "/" + Security.sha512encrypt(user.ValidationKey);

                    string emailFromTemplate = HelperFunction.RenderViewToString(this.ControllerContext, "~/Views/MailTemplates/UserActivation.cshtml", link);


                    //todo:  metod async yapılacak
                    Common.MailOperations.sendMailFORapp("WissenApp Kayıt", emailFromTemplate, user.Email);


                    return(RedirectToAction("ActivationInfo"));
                }
            }
            catch (Exception ex)
            {
                throw;
            }



            return(View());
        }