Example #1
0
        public ActionResult Login(Models.Authoritah model, string returnUrl)
        {
            // Lets first check if the Model is valid or not
            if (ModelState.IsValid)
            {
                using (Models.AuthoritahContext db = new Models.AuthoritahContext("cs_xam"))
                {
                    string login = model.login;
                    string password = model.password;

                    // User found in the database
                    if (db.Authoritahs.Any(user => user.login == login && user.password == password))
                    {

                        FormsAuthentication.SetAuthCookie(login, false);

                        // id
                        Session.Add("userID",
                            db.Authoritahs.Where(x => x.login == login).Select(x => x.id_auth).First()
                            );
                        // ФИО
                        Session.Add("fio",
                            db.Authoritahs.Where(x => x.login == login).Select(x => x.fio).First()
                            );
                        // роль
                        Session.Add("role",
                            db.Authoritahs.Where(x => x.login == login).Select(x => x.role_id).First()
                            );

                        logger.Info(string.Format("Выполнен вход с логином [{0}]", model.login));

                        if (string.IsNullOrEmpty(returnUrl))
                            { return RedirectToAction("Index", "Home"); }
                        else
                            { return Redirect(returnUrl); }
                    }
                    else
                    {
                        logger.Info(string.Format("Неудачная попытка входа с логином [{0}]", model.login));
                        ModelState.AddModelError("Ошибка входа", "Вы указали неправильный логин или пароль.");
                    }
                }
            }

            // что-то пошло не так, показать опять форму с ошибками
            return View(model);
        }
Example #2
0
        public ActionResult MyPage()
        {
            //long userID = Convert.ToInt64(Session["userID"]);
            Models.AuthoritahContext db = new Models.AuthoritahContext("cs_xam");
            var auth = (
                from a in db.Authoritahs
                    join r in db.Roles on a.role_id equals r.id_role
                where a.id_auth == userID
                select new { a.fio, a.login, r.name }
                ).First();
            Auth authModel = new Auth(auth.fio, auth.login, auth.name);

            return View("MyPage", authModel);
        }