所以我成立了我的MVC應用程序自定義登錄這似乎工作...定製登錄與MVC搞亂與登錄局部視圖
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(User model, string returnUrl)
{
if (ModelState.IsValid)
{
if (model.Login())
{
FormsAuthentication.SetAuthCookie(model.Username, true);
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "Invalid email address or password.");
}
}
// If execution got this far, something failed, redisplay form
return View(model);
}
public class User
{
public bool Login()
{
var user = db.Users.FirstOrDefault(u => u.EmailAddress == EmailAddress);
if (user == null)
{
throw new ValidationException("User not found.");
}
else
{
// validates whether or not the password on the user record
// that was retrieved by the query matches the password entered at login
return Hashing.ValidatePassword(Password, user.Password);
}
}
}
遺憾的是,它與之間的一些衝突默認_LoginPartial.cshtml
視圖(看上去像下圖):
@model LoganMVC.Models.User
@if (Request.IsAuthenticated) {
<text>
Hello, @Html.ActionLink(User.Identity.Name, "Manage", "Account", new { User.Identity.Name })!
@using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm" })) {
@Html.AntiForgeryToken()
<a href="javascript:document.getElementById('logoutForm').submit()">Log off</a>
}
</text>
} else {
<ul>
<li>@Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</li>
<li>@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>
</ul>
}
的ArgumentNullException突出這條線......
您好,@ Html.ActionLink(User.Identity.Name,「Manage」,「Account」,new {User.Identity.Name})!
說...
值不能爲空。
很顯然,這不能爲空或空值User.Identity.Name
,但有什麼不清楚的是,爲什麼IsAuthenticated
是真正開始。這是表單身份驗證,就軟件而言(因爲我殺死了調試服務器,關閉瞭解決方案並再次開始調試),應用程序從未運行過。
我明白你對電子郵件地址的意思,但你能詳細說明第一部分嗎? – Ortund
我的登錄Post方法傳遞'User user'作爲第一部分,以便通過具有電子郵件地址的我的用戶模型的實例。這就是查詢的電子郵件地址來自哪裏。在這一點上,我想我需要檢查User.Identity是否爲空,但我不知道如何做到這一點 – Ortund
現在我遇到了防僞系統的麻煩......所提供的身份鍵入'System.Web.Security。FormsIdentity'被標記爲IsAuthenticated = true,但沒有Name的值。默認情況下,防僞系統要求所有經過身份驗證的身份都有唯一的名稱。如果無法爲此標識提供唯一名稱,請考慮將靜態屬性AntiForgeryConfig.AdditionalDataProvider設置爲可爲當前用戶提供某種形式的唯一標識符的類型的實例。[ – Ortund