0
我的程序在以前工作。我不知道我做了什麼改變,但現在突然我的登錄行爲非常奇怪。每次嘗試訪問管理員授權頁面時,即使在登錄後,它也會將我重定向到登錄頁面。這裏是我的代碼:MVC 4爲什麼我的網站將我重定向到帳戶/登錄?
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(AlvinCMSExtension.Models.LoginModel model, string returnUrl)
{
string redirectUrl = returnUrl;
string userName = model.UserName;
AlvinCMSExtension.Models.UserProfile user = dbAccount.UserProfiles.Where(m => m.Email.Equals(userName, StringComparison.CurrentCultureIgnoreCase)).SingleOrDefault();
if (user != null)
{
userName = user.UserName;
}
if (ModelState.IsValid && WebSecurity.Login(userName, model.Password, persistCookie: model.RememberMe))
{
return RedirectToAction("LoginRedirectionControl", new { redirectUrl = redirectUrl });
}
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}
public ActionResult LoginRedirectionControl(string redirectUrl)
{
string returnUrl = redirectUrl;
if (redirectUrl == null)
{
redirectUrl = User.IsInRole("Admin") ? "/Admin" : "/";
}
return RedirectToLocal(redirectUrl);
}
private ActionResult RedirectToLocal(string returnUrl)
{
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
return RedirectToAction("Home", "Page");
}
我試圖訪問此:
[Authorize(Roles="Admin")]
public ActionResult Dashboard()
{
return View();
}
每次成功登錄後,Redirect(returnUrl)
不帶我去RETURNURL,而是再次登錄頁面。使用的參數是:http://localhost:5847/Account/Login?ReturnUrl=%2fAdmin%2fDashboard
。我調試代碼,並且returnUrl持有/Admin/Dashboard/
。我不知道發生了什麼。
這對我來說太無聊了。我不知道發生了什麼,但是,是的,userinrole被刪除。我沒有想到這一點,我認爲這是與iis服務器的東西。謝謝 –