当用户按下登录按钮时,控制器中的以下功能将执行,其中我使用以下方法设置用户的名字SetAuthCookie
:
// 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
它总是有空值。有什么解决办法吗?