首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >returnUrl是空的?

returnUrl是空的?
EN

Stack Overflow用户
提问于 2016-06-01 11:26:35
回答 1查看 182关注 0票数 0

我想在用户登录时将其引导到他们的配置文件页面,但returnUrlnull,我对returnUrl的工作方式有点困惑,如果有人能引导我朝正确的方向前进,非常感谢

谢谢

登录控制器:

代码语言:javascript
复制
    #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

查看:

代码语言:javascript
复制
@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>

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-01 11:44:23

尝试使用以下过载的BeginForm:

代码语言:javascript
复制
        @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>
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37567329

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档