returnUrl为null?

时间:2016-06-01 11:26:36

标签: c# asp.net-mvc

我想在用户登录时将其引导至他们的个人资料页面,但returnUrlnull,我对returnUrl如何运作感到困惑,如果有人可以引导我的话在正确的方向,非常感谢

谢谢

登录控制器:

    #region LoginAction
    [AllowAnonymous]
    public ActionResult Login(string returnUrl)
    {
        ViewBag.returnUrl = returnUrl;
        return View();
    }
    #endregion

    #region LoginPost
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginUserViewModel model,string returnUrl)
    {
        if (ModelState.IsValid)
        {
            User user = await UserManager.FindAsync(model.UserName, model.Password);
            ProfileUserViewModel userProfile = new ProfileUserViewModel
            {
                UserName = user.UserName,
                FirstName = user.FirstName,
                LastName = user.LastName,
                Weight = user.Weight,
                Goal = user.Goal,
                DailyCalories = user.DailyCalories,
                DailyCarbs = user.DailyCarbs,
                DailyFats = user.DailyFats,
                DailyProtein = user.DailyProtein

            };


            if (user == null)
            {
                ModelState.AddModelError("", "Invalid name or password");
            }
            else
            {
                ClaimsIdentity ident = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
                AuthManager.SignOut();
                AuthManager.SignIn(new AuthenticationProperties { IsPersistent = false }, ident);

                return Redirect(returnUrl);

            }

           // return View("Profile", userProfile);
        }
        ViewBag.returnUrl = returnUrl;
        return View(model);          

    }

    private IAuthenticationManager AuthManager
    {
        get { return HttpContext.GetOwinContext().Authentication; }
    }

    private AppUserManager UserManager
    {
        get
        {
            return HttpContext.GetOwinContext().GetUserManager<AppUserManager>();
        }
    }

    #endregion

查看:

@model FitnessFriend.WebUI.Models.LoginUserViewModel
@{
    ViewBag.Title = "Login";
}


<h2>Log In</h2>
@Html.ValidationSummary()

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken();

     <input type="hidden" name="returnUrl" value="@ViewBag.returnUrl" />

    <div class="form-group">
        <label>Name</label>
        @Html.TextBoxFor(x => x.UserName, new { @class = "form-control" })
    </div>

    <div class="form-group">
        <label>Password</label>
        @Html.PasswordFor(x => x.Password, new { @class = "form-control" })
    </div>

    <button class="btn btn-primary" type="submit">Log In</button>

}

1 个答案:

答案 0 :(得分:0)

尝试使用BeginForm的以下重载:

        @using (Html.BeginForm("Login", "Account",new { returnUrl = ViewBag.returnUrl }, FormMethod.Post ))
    {
 @Html.AntiForgeryToken();


    <div class="form-group">
        <label>Name</label>
        @Html.TextBoxFor(x => x.UserName, new { @class = "form-control" })
    </div>

    <div class="form-group">
        <label>Password</label>
        @Html.PasswordFor(x => x.Password, new { @class = "form-control" })
    </div>

    <button class="btn btn-primary" type="submit">Log In</button>
}