2

我正在使用 mvc4 应用程序内置的帐户注册和登录控制器(及其各自的模型)。我有自己的删除帐户的方法。所以我想在标准登录控制器中进行检查,以便它检查我是否自己删除了该帐户,然后将该人重定向到一个信息页面。

但是,当控制器被调用时,它只会传递我的代码行并仍然让用户登录。我已经检查了数据库。用户将 0 设置为其 IsActive(将帐户列为“活动”和“非活动”)值,应该被重定向。但他只是像我的代码行不存在一样登录......

这是控制器中的代码:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
 if (WorkerDAL.IsActive(model.UserName)) RedirectToAction("DisplayDeletedUserMessage", "Home");    //my redirect code

 if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
         {
                    return RedirectToLocal(returnUrl);
         }

     // If we got this far, something failed, redisplay form
 ModelState.AddModelError("", "The user name or password provided is incorrect.");
 return View(model);
}

我的 DAL 类的代码:

public static bool IsActive(string _username)
{  TimeTrackerEntities db = new TimeTrackerEntities();
   foreach (Worker worker in db.Workers)
   {
       if (worker.UserName == _username)
       {
             if (worker.IsActive == 1) return true;
             if (worker.IsActive == 0) return false;
       }
   }
return true;
}

我是否对用户使用了错误的操作来进行重定向?或者像这样明显的东西?我基本迷路了...

编辑 - 感谢 Eric J. 和 Jonathon Page,代码现在可以正常工作。谢谢你们俩!我怎样才能将他们的两个评论都记为正确答案?

4

2 回答 2

2

I assume this line:

if (WorkerDAL.IsActive(model.UserName)) RedirectToAction("DisplayDeletedUserMessage", "Home");

should read

if (!WorkerDAL.IsActive(model.UserName)) RedirectToAction("DisplayDeletedUserMessage", "Home");

It looks like you're currently redirecting to display the deleted user message if the user is active.

于 2013-02-17T02:38:12.793 回答
0

您缺少 return 关键字:

RedirectToAction("DisplayDeletedUserMessage", "Home"); //my redirect code

应该读:

return RedirectToAction("DisplayDeletedUserMessage", "Home"); //my redirect code

于 2013-02-17T11:32:03.873 回答