من از لاگین به روش membership استفاده کردم.
میخوام سشن کاربر رو در layout یا در صفحات دیگه نگه دارم. و همینطور اگر کاربر لاگین کرده گزینه logout هم براش نمایش داده بشه.
اما بعد از لاگین سشن ایجاد نمیشه. (و بدنبال اون layout)
از این دو دستور استفاده میکنم اما مقدارش بعد از لاگین null هست.
برای نشان دادن نام کاربر لاگین شده
@User.Identity.Name
برای نمایش logout به کاربر
@if (User.Identity.IsAuthenticated)
{
<a href="/Home/Logout" class="btn btn-default btn-flat">خروج از سایت</a>
}
البته خودم سرچ کردم چیزای متفاوتی دیدم ولی فکر میکنم این روش از همه ساده تر هست و جواب میده.
این هم کدهای کنترلر (اگه لازم شد)
[HttpGet]
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(string username,string password,bool rememberme)
{
if (BlUser.Exist(username, password))
{
FormsAuthentication.SetAuthCookie(username,remembe rme);
return RedirectToAction("Index");
}
else
{
ViewBag.message = "نام کاربری یا پسورد اشتباه است";
}
return View();
}
public ActionResult Logout()
{
FormsAuthentication.SignOut();
return RedirectToAction("Index");
}