简体   繁体   English

ASP.NET MVC 4错误的重定向

[英]ASP.NET MVC 4 Wrong redirection

I have created a new action to confirm user account by a token sent in e-mail. 我创建了一个新操作,通过电子邮件中发送的令牌确认用户帐户。 It looks like this: 它看起来像这样:

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

And example link to this action would be: localhost:57904/Account/ConfirmToken/ubiJScfyP9zM1WUPCdb54Q2/ 示例链接到此操作将是:localhost:57904 / Account / ConfirmToken / ubiJSffyP9zM1WUPCdb54Q2 /

The problem is, I never ever get redirected to said Index action from Home controller. 问题是,我永远不会被重定向到Home控制器的Index动作。 I constantly get redirected to Account/Login with previous link as a return URL in parameter. 我不断被重定向到帐户/登录,前一个链接作为参数中的返回URL。 Doesn't matter what I add in the code. 无论我在代码中添加什么。

  • when I remove an entire ConfirmToken action, then I get an error 当我删除整个ConfirmToken动作时,我收到一个错误
  • when there is an action with nothing in its body, even then I am redirected to Account/Login 当行动中没有任何内容时,即使这样,我也会被重定向到帐户/登录

I am new to this ASP.NET MVC 4 concept, perhaps I am not doing something properly..? 我是这个ASP.NET MVC 4概念的新手,也许我没有做正确的事情......? I'm using Visual Studio 2012. 我正在使用Visual Studio 2012。

EDIT: I don't know if there's a problem with the code itself. 编辑:我不知道代码本身是否存在问题。 It's an empty project I basically created a few hours ago and made minor modifications to the user registration process. 这是我几个小时前基本上创建的一个空项目,并对用户注册过程进行了少量修改。 It feels more like the code doesn't refresh since it DID contain redirection to Account/Login in the first place but then I wanted to change it. 感觉更像代码没有刷新,因为它首先包含重定向到帐户/登录,但后来我想改变它。

EDIT2: Here's my Index/Home action 编辑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();
    }

You are facing an authentication issue try logging in before going confirming it will not redirect you the the login view. 您正面临身份验证问题尝试登录,然后确认它不会重定向您的登录视图。

If you decorated the class with [Authorize] then you need to allow all user on the controller action, otherwise it will keep redirecting you. 如果您使用[Authorize]修饰了类,则需要允许所有用户执行控制器操作,否则它将继续重定向您。

[Authorize]
public class ConfirmController : Controller {

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM