HI ROHITH, I AM NEW FOR MVC. PLEASE EXPLAIN HOW TO SET IMPLICITLY ISAUTHENTICATED TRUE.

venkat gosetty

Total Post:15

Points:105
Posted by  venkat gosetty
 3167  View(s)
Ratings:
Rate this:
Hi Rohith,

    Please explain how to set implicitly IsAuthenticated true. If you have code please post me.

Thank you

  1. Chris Anderson

    Post:65

    Points:455
    Re: Hi Rohith, I am new for mvc. Please explain how to set implicitly IsAuthenticated true.

    Hi,

    Could you explain me where you have store the user information either in Membership or database.
    If you are using Membership to store user information, you can refer demo application provided by mvc in visual studio.
    If you are using database, you can build your own logic to authenticate user.
    If not able to understand paste your code here...

    Thanks.

  1. venkat gosetty

    Post:15

    Points:105
    Re: Hi Rohith, I am new for mvc. Please explain how to set implicitly IsAuthenticated true.

    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);
            }

  1. Chris Anderson

    Post:65

    Points:455
    Re: Hi Rohith, I am new for mvc. Please explain how to set implicitly IsAuthenticated true.

    So, finally you got your answer?

  1. venkat gosetty

    Post:15

    Points:105
    Re: Hi Rohith, I am new for mvc. Please explain how to set implicitly IsAuthenticated true.

    yes Rohith i got that. But i am getting welcome To as Username when i logged. I want display FirstName Field after logged.

  1. Chris Anderson

    Post:65

    Points:455
    Re: Hi Rohith, I am new for mvc. Please explain how to set implicitly IsAuthenticated true.

    So, store the first name when user logged in, either create a property or a ViewData property wherever you want to store and display it.

  1. venkat gosetty

    Post:15

    Points:105
    Re: Hi Rohith, I am new for mvc. Please explain how to set implicitly IsAuthenticated true.

    Hi Rohith,

                  I want display in master page.

        if (Request.IsAuthenticated) {
    %>
            Welcome <b><%: Page.User.Identity.Name %></b>!
            <%: Html.ActionLink("Log Off", "LogOff", "Account") %>
          
    <%
        }

  1. Chris Anderson

    Post:65

    Points:455
    Re: Hi Rohith, I am new for mvc. Please explain how to set implicitly IsAuthenticated true.

    First store user name:

    ViewBag.UserName="assign user name";

    then display it:

    if (Request.IsAuthenticated)
    {
    %>
            Welcome <b><%: ViewBag.UserName %></b>!
            <%: Html.ActionLink("Log Off", "LogOff", "Account") %>
          
    <%
    }

    how you set set Request.IsAuthenticated to true when you are using database as a medium to store data?

  1. venkat gosetty

    Post:15

    Points:105
    Re: Hi Rohith, I am new for mvc. Please explain how to set implicitly IsAuthenticated true.

    I have written code for Request.IsAutheticated 

    [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();
                            ViewData["Name"] = model.FirstName;
                            return RedirectToAction("Dashboard", "SuperAdmin");
                        }
                        else if (model.RoleId == 2)
                        {
                            model.IsLoggedIn();
                            return RedirectToAction("Dashboard", "Admin");
                        }
                        else if (model.RoleId == 3)
                        {
                            model.IsLoggedIn();
                            return RedirectToAction("Dashboard", "AppHome");
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "EmailId or Password do not match");
                    }
                }
                return View(model);
            }


    and my .ascx

    if (Request.IsAuthenticated) {
    %>
            Welcome <b><%: ViewData["Name"] %></b>!
            <%: Html.ActionLink("Log Off", "LogOff", "Account") %>
          
    <%
        }

    not display the username and also no error

  1. Chris Anderson

    Post:65

    Points:455
    Re: Hi Rohith, I am new for mvc. Please explain how to set implicitly IsAuthenticated true.

    Have you checked Request.IsAuthenticated returns true or false. If it returns true then it will only display user name.

    Write the below code without if condition and then check whether it displays or not:

    Welcome <b><%: ViewData["Name"] %></b>!

  1. venkat gosetty

    Post:15

    Points:105
    Re: Hi Rohith, I am new for mvc. Please explain how to set implicitly IsAuthenticated true.

    it's not working

  1. venkat gosetty

    Post:15

    Points:105
    how to validate validations whether textbox is empty or not. using linq to sql.

    hi,
       how to validate validations whether textbox is empty or not. using linq to sql.

Answer

NEWSLETTER

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