【问题标题】:ASP.NET MVC 4 Wrong redirectionASP.NET MVC 4 错误的重定向
【发布时间】:2013-10-21 17:18:40
【问题描述】:

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

    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。我在代码中添加什么都没关系。

  • 当我删除整个 ConfirmToken 操作时,我得到一个错误
  • 当有一个动作在其主体中没有任何内容时,即使这样我也会被重定向到帐户/登录

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

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

EDIT2:这是我的索引/主页操作

    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();
    }

【问题讨论】:

  • Index/Home 是否有 [Authorize] 属性?当您检查您的网络请求时,它是尝试转到 Home/Index 还是直接转到 Account/Login
  • 不,它没有任何属性
  • 你在使用表单认证吗?
  • 是的,我的 Web.config 显示
  • 您的 Index:Home 操作是什么样的?

标签: c# asp.net asp.net-mvc-4 redirect


【解决方案1】:

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

如果你用[Authorize] 装饰了这个类,那么你需要允许所有用户在控制器上操作,否则它会一直重定向你。

[Authorize]
public class ConfirmController : Controller {

  [AllowAnonymous]  
  public ActionResult ConfirmToken(string id)
  {
   //..
  }

}

【讨论】:

  • 工作就像一个魅力!我不知道这可能是原因!
  • 我喜欢第一条评论询问 [Authorize] 属性的方式,他拒绝了。
猜你喜欢
  • 2017-06-14
  • 1970-01-01
  • 1970-01-01
  • 2014-06-20
  • 1970-01-01
  • 2013-06-03
  • 2018-05-02
  • 2012-02-08
  • 1970-01-01
相关资源
最近更新 更多