简体   繁体   中英

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/

The problem is, I never ever get redirected to said Index action from Home controller. I constantly get redirected to Account/Login with previous link as a return URL in parameter. Doesn't matter what I add in the code.

  • when I remove an entire ConfirmToken action, then I get an error
  • 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..? I'm using 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

    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]
public class ConfirmController : Controller {

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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