Home > DeveloperSection > Forums > Hi Rohith, I am new for mvc. Please explain how to set implicitly IsAuthenticated true.
venkat gosetty
venkat gosetty

Total Post:15

Points:105
Posted on    November-02-2011 9:41 PM

 ASP.NET MVC ASP.NET MVC 
Ratings:


 11 Reply(s)
 2814  View(s)
Rate this:
Hi Rohith,

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

Thank you



Chris Anderson
Chris Anderson

Total Post:65

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

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.

venkat gosetty
venkat gosetty

Total Post:15

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

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

Chris Anderson
Chris Anderson

Total Post:65

Points:455
Posted on    November-03-2011 8:49 AM

So, finally you got your answer?

venkat gosetty
venkat gosetty

Total Post:15

Points:105
Posted on    November-07-2011 11:55 PM

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

Chris Anderson
Chris Anderson

Total Post:65

Points:455
Posted on    November-08-2011 12:14 AM

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.

venkat gosetty
venkat gosetty

Total Post:15

Points:105
Posted on    November-09-2011 1:58 AM

Hi Rohith,

              I want display in master page.

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

Chris Anderson
Chris Anderson

Total Post:65

Points:455
Posted on    November-09-2011 2:40 AM

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?

venkat gosetty
venkat gosetty

Total Post:15

Points:105
Posted on    November-09-2011 3:11 AM

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

Chris Anderson
Chris Anderson

Total Post:65

Points:455
Posted on    November-09-2011 5:25 AM

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>!

venkat gosetty
venkat gosetty

Total Post:15

Points:105
Posted on    November-10-2011 1:48 AM

it's not working

venkat gosetty
venkat gosetty

Total Post:15

Points:105
Posted on    November-18-2011 3:26 AM

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

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

Follow MindStick