---
title: "Logon and Logoff  user controls. How to set IsAuthenticated is true?"  
description: "Logon and Logoff  user controls. How to set IsAuthenticated is true?"  
author: "venkat gosetty"  
published: 2011-11-02  
updated: 2011-11-02  
canonical: https://www.mindstick.com/forum/343/logon-and-logoff-user-controls-how-to-set-isauthenticated-is-true  
category: "asp.net mvc"  
tags: ["asp.net mvc"]  
reading_time: 2 minutes  

---

# Logon and Logoff  user controls. How to set IsAuthenticated is true?

\
How to IsAuthenticated is set to true?\
[Please check](https://www.mindstick.com/forum/348/hi-rohith-i-post-the-code-please-check-it-thank-you) this code i have written in app.\
\
Model\
\
[Required(ErrorMessage = "Email Id Required")]\
[DisplayName("Email ID")]\
[[RegularExpression](https://www.mindstick.com/forum/160247/what-is-the-purpose-of-the-regularexpression-data-annotation)(@"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", ErrorMessage = "Email Format is wrong")]\
[[StringLength](https://www.mindstick.com/forum/160244/how-to-use-stringlength-to-limit-the-length-of-a-string-property-in-a-model)(50, ErrorMessage = "Less than 50 [characters](https://answers.mindstick.com/qa/42112/who-is-the-originator-of-avengers-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](https://www.mindstick.com/interview/33973/how-to-split-the-full-name-into-firstname-and-lastname-in-sql-server) { get; set; }\
\
public bool IsUserExist(string emailid, string password)\
{\
bool flag= false;\
con.Open();\
SqlDataReader dr;\
[SqlCommand](https://www.mindstick.com/forum/91/difference-between-sqlcommand-and-sqlcommandbuilder) 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](https://www.mindstick.com/blog/273/passing-values-from-controller-to-view-in-asp-dot-net-mvc)\
\
[HttpPost, ValidateInput(false)]\
public [ActionResult](https://www.mindstick.com/forum/33961/what-is-the-difference-between-actionresult-and-viewresult) LogOn(Login model)\
{\
if ([ModelState.IsValid](https://www.mindstick.com/forum/2002/modelstate-isvalid-contains-errors-when-using-mongodb))\
{\
if (model.IsUserExist(model.EmailId, model.Password))\
{\
model.FirstName = User.Identity.Name;\
if (model.RoleId == 1)\
{\
model.IsLoggedIn();\
return [RedirectToAction](https://www.mindstick.com/forum/2342/how-to-redirecttoaction-with-anchor-tag-in-mvc)("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);\
}

## Replies

### Reply by Chris Anderson

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.\


---

Original Source: https://www.mindstick.com/forum/343/logon-and-logoff-user-controls-how-to-set-isauthenticated-is-true

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
