Reputation: 317
Hello everyone I'm trying to share object values using Sessions across different controllers, I'm kind of lost because the way I handle authentication in my website is not described in any article nor guide I can refer to in order to follow up a "how-to"
Anyways I have a BaseController that is shared among other Controllers that has the "BaseController.verifyAccess(string user, string password)", what it does is, it goes and check in my DB if the user has access to the View
BaseController.cs
public class User
{
public int ID { get; set; }
public string username { get; set; }
public string password { get; set; }
public string email { get; set; }
}
AccountController.cs
[HttpPost]
public ActionResult Login(FormCollection form)
{
User userTest = new User();
userTest.username = form["username"];
userTest.password = form["password"];
Session["username"] = userTest.username;
if (verifyAccess(userTest.username, userTest.password))
{
return RedirectToAction("../LoggedIn");
}
else
{
return RedirectToAction("../Login");
}
}
How can I get Session["username"] = userTest.username;
to work in other Controllers??
For example
SomeController.cs
[HttpPost]
public ActionResult MyAwesomeView(FormCollection form)
{
Project.Models.MyAwesomeModel thisModel = new Models.MyAwesomeModel();
thisModel._info1 = form["txtInfo1"];
thisModel._info2 = form["txtInfo2"];
thisModel._info3 = form["txtInfo3"];
thisModel._info4 = form["txtInfo4"];
thisModel._user = System.Web.HttpContext.Current.Session["username"].ToString();
return View(modelPagosRecibidos);
}
While debugging thisModel._user
is null, plus I think I'm doing this heavily wrong, I apologize if someone sighs at my code, anyways hope to get some help and guidance on how to get Session values across my Controllers, thanks in advance to everyone!
Upvotes: 1
Views: 1964
Reputation: 22409
Try below code snippet. Once you assign your information on Application Session
it will be reachable on every Client Controller
which you can pick up from that session like the example I have showed.
public class DeshboardController : Controller
{
// GET: Admin
public ActionResult Index()
{
string roleid = (string)Session["user_role_id"];
string userid = (string)Session["user_au_id"];
string companyId = (string)Session["company_id"];
string accessToken = (string)Session["AccessToken"];
string ConName = "Deshboard";
string ActionName = "index";
if (string.IsNullOrEmpty(roleid) || string.IsNullOrEmpty(userid) || string.IsNullOrEmpty(companyId))
{
Response.Redirect("/Login/Index");
}
bool permission = CoreRules.UserPermission(roleid, userid, ConName, ActionName, accessToken);
if (!permission)
{
try
{
Response.Redirect("/Error/Index");
}
catch (Exception ex)
{
Response.Redirect("/Login/Index");
}
}
return View();
}
}
Additionally you could set your token validity on Web.config
file like below
<appSettings>
<add key="TokenExpiry" value="45" />
</appSettings>
I have set token time for 45 minutes. You could customize as per your requirement.
Upvotes: 1