LOGON AND LOGOFF USER CONTROLS. HOW TO SET ISAUTHENTICATED IS TRUE?

venkat gosetty

Total Post:15

Points:105
Posted by  venkat gosetty
 16860  View(s)
Ratings:
Rate this:

How to IsAuthenticated is set to true?
Please check this code i have written in app.

Model

        [Required(ErrorMessage = "Email Id Required")]
        [DisplayName("Email ID")]
        [RegularExpression(@"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", ErrorMessage = "Email Format is wrong")]
        [StringLength(50, ErrorMessage = "Less than 50 characters")]

        public string EmailId { get; set; }

        [DataType(DataType.Password)]
        [Required(ErrorMessage = "Password Required")]
        [DisplayName("Password")]
        [StringLength(30, ErrorMessage = ":Less than 30 characters")]

        public string Password { get; set; }

        public int RoleId { get; set; }

        public string FirstName { get; set; }

        public bool IsUserExist(string emailid, string password)
        {
            bool flag= false;
            con.Open();
            SqlDataReader dr;
            SqlCommand cmd = new SqlCommand("select count(*) from Security.Users where UserName='" + emailid + "' and Password='" + password + "'", con);
            SqlCommand cmd1 = new SqlCommand("select RoleId from security.Users where UserName='" + emailid + "'", con);
            flag= Convert.ToBoolean(cmd.ExecuteScalar());
            dr = cmd1.ExecuteReader();
            if (dr.Read() == true)
            {
                RoleId = Convert.ToInt32(dr[0].ToString());
            }
            dr.Close();
            con.Close();
            return flag;
        }

Controller

[HttpPost, ValidateInput(false)]
        public ActionResult LogOn(Login model)
        {
            if (ModelState.IsValid)
            {
                if (model.IsUserExist(model.EmailId, model.Password))
                {
                    model.FirstName = User.Identity.Name;
                    if (model.RoleId == 1)
                    {
                        model.IsLoggedIn();
                        return RedirectToAction("Index", "SuperAdmin");
                    }
                    else if (model.RoleId == 2)
                    {
                        model.IsLoggedIn();
                        return RedirectToAction("Home", "Admin");
                    }
                    else if (model.RoleId == 3)
                    {
                        model.IsLoggedIn();
                        return RedirectToAction("Index", "AppHome");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "EmailId or Password do not match");
                }
            }
            ViewData["RoleId"] = model.RoleId;
            ViewData["FirstName"] = model.FirstName;
            return View(model);
        }
  1. Chris Anderson

    Post:65

    Points:455
    Re: Logon and Logoff user controls. How to set IsAuthenticated is true?

    Hi venket,

    You cannot explicitly set Request.IsAuthenticated to true because it is read only.
    Request.IsAuthenticated automatically sets true when you call FormsAuthentication.SetAuthCookie(userName, bool); and it automatically set false when you call FormsAuthentication.SignOut();
    If you want to set IsAuthenticated property to true call SetAuthCookie function of the FormAuthentication with necessary parameters.

    Thanks.

      Modified On Nov-02-2011 04:28:01 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!