當用戶按下登錄按鈕時,按照控制器功能執行中,我使用SetAuthCookie
設置用戶名:MVC3 @ Context.User.Identity.Name空值
// POST: /Account/SignUp
[HttpPost]
public ActionResult Index(ConsumerView consumerData)
{
try
{
ConsumerManager consumerManagerObj = new ConsumerManager();
if (consumerManagerObj.IsValidUser(consumerData.LoginID, consumerData.Password))
{
FormsAuthentication.SetAuthCookie(consumerData.FirstName, false);
return View ("WelcomeConsumer");
}
else
{
ModelState.AddModelError("", "Invalid Username/Password!!");
}
}
catch
{
return View(consumerData);
}
return View(consumerData);
}
WelcomeConsumer
視圖具有代碼:
@{
ViewBag.Title = "Home";
}
<h2>Welcome</h2>
<h2>Hi <b>@Context.User.Identity.Name</b></h2>
@Html.ActionLink("[Sign Out]", "SignOut", "ConsumerAccount")
<p>
<ul>
<li>@Html.ActionLink("Book new Ticket", "NewBooking", "ConsumerAccount")</li>
<li>@Html.ActionLink("View all Bookings", "ViewAllBookings", "ConsumerAccount") </li>
</ul>
@ViewBag.NewBookingSuccess
</p>
但是@Context.User.Identity.Name
總是有空值。任何解決方案
您使用哪種授權? – ssilas777
我是MVC的新手。請解釋 – Azeem
http://msdn.microsoft.com/en-us/library/aa291347(v=vs.71).aspx請檢查此 – ssilas777