簡體   English   中英

MVC4 WebSecurity.Login第一次失敗,無法通過Login方法獲得User

[英]MVC4 WebSecurity.Login fails first time through, can't get User in Login method

好的,一個下午,我向SO表示了幫助。 我的登錄方法非常標准。 如下所示,我正在使用WebSecurity.Login。

在那之后,我想進行檢查以查看用戶的個人資料是否已完成,如果尚未完成,請將其發送到該視圖。

調節器

[AllowAnonymous]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
    if (ModelState.IsValid && WebSecurity.Login(model.Email, model.Password, persistCookie: model.RememberMe))
    {

        if (!User.IsInRole("User"))
        {
            if (!IsProfileComplete(WebSecurity.GetUserId(User.Identity.Name)))
            {
                return RedirectToAction("ProfileCompletion");
            }

            if(!User.IsInRole("User")) Roles.AddUserToRole(User.Identity.Name, "Vendor");
        }
        else // user has role
        {
            return RedirectToAction("Index", "Dashboard");
        }
    }

    ModelState.AddModelError("", "Unknown username or password.");
    return View(model);
}

我已經完成了一堆.... WebSecurity.Login之后,我印象中將設置WebSecurity用戶數據,但是WebSecurity.GetUserId(User.Identity.Name)返回為-1

即使用戶已完成配置文件,它也會將其重定向到配置文件完成,因為它正在嘗試查找用戶ID -1的配置文件

我缺少有關http帖子和登錄上下文集的信息嗎? 我要尋找的最終結果是確保用戶進入系統之前已完成個人資料頁面。 也許有更好的方法可以做到這一點?

編輯 - - -

找到了此鏈接,但如果有人可以為我所需的功能提出更好的模式,我還是希望快速發表評論

MVC 4 SimpleMembership-登錄后為何選擇WebSecurity.CurrentUserId -1

正如您發現的帖子指出的那樣,登錄后不會立即設置用戶身份,因此User.Identity.Name不包含用戶名。 請改用model.UserName。 嘗試更改此行:

if (!IsProfileComplete(WebSecurity.GetUserId(User.Identity.Name)))

對此:

if (!IsProfileComplete(WebSecurity.GetUserId(model.UserName)))

暫無
暫無

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

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