Home > DeveloperSection > Forums > Hi Rohith, I post the code please check it. Thank you
venkat gosetty
venkat gosetty

Total Post:15

Points:105
Posted on    November-03-2011 8:41 AM

 ASP.NET MVC ASP.NET MVC 
Ratings:


 1 Reply(s)
 1536  View(s)
Rate this:
I am using sqlserver database

Model

public class Login
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["EMM360ConnectionString"].ConnectionString);

        [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

Recently i added the FormsAuthentication.SetAuthCookie(model.EmailId, true); Now it is working fine.



[HttpPost, ValidateInput(false)]
        public ActionResult LogOn(Login model)
        {
            if (ModelState.IsValid)
            {
                if (model.IsUserExist(model.EmailId, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.EmailId, true);
                    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");
                }
            }
            return View(model);
        }


Thank you
Venkat


Chris Anderson
Chris Anderson

Total Post:65

Points:455
Posted on    November-03-2011 9:17 AM

hi,

Pls. do not create new thread for the same type of questions again and again. Use reply button of forum until you got your answer.
So take care next time and use reply to ask the same type of question or for replying to us.

Thanks.

Don't want to miss updates? Please click the below button!

Follow MindStick