0

我一直在这里搜索一些示例,但找不到我需要的东西,所以我在这里发布我的问题:

我正在使用表单身份验证和 MVC 4.5。

问题如下:

我正在通过电子邮件将 URL 发送给用户,用户需要单击 URL 并在通过身份验证后能够重定向到该 URL。

我的登录控制器有一个方法索引,当用户尝试登录时会调用它:

[HttpPost]
public ActionResult Index(LoginModel model, string returnUrl)
{
    if (ModelState.IsValid)
    {
        try
        {
            LoginModel lm = LoginModel.AuthenticateUser(model.UserName, model.Password);
            if (lm.IsAuthenticated)
            {
                ApplicationSession.SetUser(lm.User);
                FormsAuthentication.SetAuthCookie(model.UserName, false);

                if (lm.User.ChangePassword)
                {
                    return Redirect(returnUrl ?? Url.Action("ForceChangePassword", "ChangePassword"));
                }
                else
                {
                    return Redirect(returnUrl ?? Url.Action("Index", "Details"));
                }
            }
            else
            {
                if (lm.GroupPermissionMessage.Length > 0)
                {
                    ModelState.AddModelError("", lm.GroupPermissionMessage);
                }
                else
                {
                    ModelState.AddModelError("", lm.SaveResult.ErrorDescription);
                }

                return View();
            }
        }
        catch(Exception error)
        {
            ModelState.AddModelError("", error.Message);
            return View();
        }
    }
    else
    {
        return View();
    }
}

登录表单具有以下定义:

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "LoginForm", returnUrl = Request.QueryString["ReturnUrl"] }))

这是我为电子邮件生成 URL 的逻辑:

 var queryString = @Url.Action("Index", "Details", new System.Web.Routing.RouteValueDictionary(new { id = model.Ticket.TicketId }), "http", Request.Url.Host);
                    var ticketUrl = "<a href='"
                                    + @Url.Action("Index", "Detail", new System.Web.Routing.RouteValueDictionary(new { id = model.Ticket.TicketId, returnUrl = queryString }), "http", Request.Url.Host)
                                    + "'>Go to your ticket</a>";

我希望Request.QueryString["ReturnURL"]与我从电子邮件中打开的 URL 相同,因此我可以在控制器的 Index 方法中重定向到它。

现在,如果我已经通过身份验证,我可以导航到该 URL。但是,如果我已注销并尝试导航到该 URL,则会首先打开登录页面。然后我需要登录,并且应该会自动重定向到我在电子邮件中单击的 URL。

我怎样才能做到这一点?

我在这里想念什么?

4

0 回答 0