ASP.NET MVC 4 重定向错误

本文关键字:重定向 错误 MVC NET ASP | 更新日期: 2023-09-27 18:36:20

我创建了一个新操作,以通过电子邮件发送的令牌来确认用户帐户。它看起来像这样:

    public ActionResult ConfirmToken(string id)
    {
        bool isConfirmed;
        isConfirmed = WebSecurity.ConfirmAccount(id);
        if (isConfirmed)
        {
            return RedirectToAction("Index", "Home", new { Message = ManageMessageId.ConfirmSuccess });
        }
        else
        {
            return RedirectToAction("Index", "Home", new { Message = ManageMessageId.ConfirmFail });
        }
    }

此操作的示例链接是:localhost:57904/account/ConfirmToken/ubiJScfyP9zM1WUPCdb54Q2/

问题是,我从来没有从Home控制器重定向到上述索引操作。我经常被重定向到帐户/登录,并带有以前的链接作为参数中的返回 URL。我在代码中添加什么并不重要。

  • 当我删除整个确认令牌操作时,出现错误
  • 当一个动作中没有任何内容时,即使这样我也会被重定向到帐户/登录

我是这个 ASP.NET MVC 4概念的新手,也许我没有做正确的事情..?我正在使用Visual Studio 2012。

编辑:我不知道代码本身是否有问题。这是我几个小时前创建的空项目,并对用户注册过程进行了细微的修改。感觉更像是代码没有刷新,因为它首先确实包含重定向到帐户/登录名,但后来我想更改它。

编辑2:这是我的索引/主页操作

    public ActionResult Index(ManageMessageId? message)
    {
        ViewBag.StatusMessage =
            message == ManageMessageId.RegisterSuccess ? "An e-mail has been sent to the e-mail address you provided. It contains instructions how to confirm your account and finish the registration process. If you cannot see the e-mail in your inbox, please check spam folder."
            : message == ManageMessageId.ConfirmSuccess ? "Your account has been successfully confirmed and you can now login."
            : message == ManageMessageId.ConfirmFail ? "An error occured while activating your account. Please mail our support for assistance."
            : "";
        return View();
    }

ASP.NET MVC 4 重定向错误

您面临身份验证问题 在确认它不会重定向您登录视图之前,请尝试登录。

如果您用[Authorize]装饰了类,则需要允许所有用户对控制器进行操作,否则它会不断重定向您。

[Authorize]
public class ConfirmController : Controller {
  [AllowAnonymous]  
  public ActionResult ConfirmToken(string id)
  {
   //..
  }
}