簡體   English   中英

returnUrl為空嗎?

[英]returnUrl is null?

我想在用戶登錄時將用戶定向到他們的個人資料頁面,但是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>

}

嘗試使用下面的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>
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM