Within a navbar menu, I'm creating a dropdown menu that contains the LoginPartial.
Here's the LoginPartial code:
<li class="nav-item dropdown">
<a href="#" id="LoginDropdownLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" role="button" class="nav-link dropdown-toggle fa fa-user-circle"></a>
<div class="dropdown-menu" aria-labelledby="LoginDropdownLink">
@using (Html.BeginForm("Login", "Account", new { ReturnUrl = "../../Home/Account" }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.TextBoxFor(m => m.Email, new { @class = "input-text", @placeholder = "Email Address" })
@Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" })
@Html.PasswordFor(m => m.Password, new { @class = "input-text", @placeholder = "Password" })
@Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe)
<input type="submit" value="Log in" class="btn btn-default" />
}
</div>
And here's how I'm including it:
<ul class="nav navbar-nav navbar-right">
<li><a class="active" href="../../Home/Index">Home</a></li>
<li><a href="../../Home/Build">Build A Box</a></li>
<li><a href="#ContactUs">Contact Us</a></li>
@Html.Action("LoginPartial", "Account")
</ul>
Here's the controller code:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
return View(model);
}
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, change to shouldLockout: true
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
if (model.OrderId != null)
{
Orders order = db.Orders.Where(x => x.OrderId == model.OrderId).First();
order.UserId = User.Identity.GetUserId();
db.SaveChanges();
return RedirectToAction("Checkout1", "Home", new { model.OrderId });
}
else
{
return RedirectToAction("Index", "Account");
}
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.RequiresVerification:
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
case SignInStatus.Failure:
default:
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
}
}
I put a breakpoint on the var result=... line, and found that the result is "Success." However, rather than loading the Account/Index page, I get redirected to the login page (with the return URL):
I'm including the Account/Index controller code for completeness. I added an [AllowAnonymous] attribute to it temporarily to see what would happen, and it did load (but the model contained no data, of course).
public ActionResult Index()
{
AccountIndexViewModel model = new AccountIndexViewModel();
var Current = User.Identity.GetUserId();
model.Orders = db.Orders.Where(x => x.UserId == Current).OrderByDescending(x=> x.Date).ToList();
List<string> OrderIds = db.Orders.Where(x => x.UserId == Current).Select(x => x.OrderId).ToList();
model.OrderDetails = db.OrderDetail.Where(x=> OrderIds.Contains(x.OrderId)).ToList();
return View(model);
}