User.Identity.GetUserId() 返回 null

Pas*_*ali 5 c# asp.net authentication asp.net-mvc

当我使用User.Identity.GetUserId()方法时,它返回 null。我正在尝试创建一种方法(基于AccountControllerMVC)来更改用户的密码。问题在于User.Identity.GetUserId()返回 null。

看看我的控制器,如果可以的话请帮忙。

开始和构造函数

protected ApplicationDbContext ApplicationDbContext { get; set; }
    protected UserManager<ApplicationUser> UserManager { get; set; }

    public ClienteController()
    {
        this.ApplicationDbContext = new ApplicationDbContext();
        this.UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(this.ApplicationDbContext));
        //this.UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
    }
Run Code Online (Sandbox Code Playgroud)

登录方式

[HttpPost]
    [AllowAnonymous]
    public ActionResult Login(ClienteViewModel model, string returnUrl)
    {
        Cliente cliente = ChecarAcesso(model);

        if (cliente != null)
        {
            var claims = new List<Claim>();
            claims.Add(new Claim(ClaimTypes.Name, cliente.nomeCompletoCliente));
            claims.Add(new Claim(ClaimTypes.Email, cliente.emailCliente));

            var id = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);

            var ctx = Request.GetOwinContext();

            var authenticationManager = ctx.Authentication;
            authenticationManager.SignOut();
            authenticationManager.SignIn(id);
            Session.Add("cliente", cliente);

            if (returnUrl != null)
            {
                if (Url.IsLocalUrl(returnUrl))
                    return Redirect(returnUrl);
            }

            return RedirectToAction("Index", "Home");
        }
        else
        {
            return RedirectToAction("AcessoNegado");
        }
    }
Run Code Online (Sandbox Code Playgroud)

管理方法不起作用

[HttpPost]
    [Authorize]
    //[ValidateAntiForgeryToken]
    public async Task<ActionResult> Gerenciar(GerenciarClienteViewModel model)
    {
        //bool hasPassword = HasPassword();
        //ViewBag.HasLocalPassword = hasPassword;
        ViewBag.ReturnUrl = Url.Action("Gerenciar");
        //if (hasPassword)
        //{
            if (ModelState.IsValid)
            {
                string x = User.Identity.GetUserId();
                IdentityResult result = await UserManager.ChangePasswordAsync(User.Identity.GetUserId(), model.senhaAntiga, model.novaSenha);
                if (result.Succeeded)
                {
                    return RedirectToAction("Gerenciar", new { Message = ManageMessageId.ChangePasswordSuccess });
                }
                else
                {
                    AddErrors(result);
                }
            }
        //}

        // If we got this far, something failed, redisplay form
        return View(model);
    }
Run Code Online (Sandbox Code Playgroud)

Pas*_*ali 1

我找到了一种适合我的方法。我使用会话获取用户的 ID,但我发现该方法ChangePasswordAsync()尝试在数据库上创建新表。就我而言,我已经有一个数据库。所以我只是创建一个方法来接收带有用户 ID 的会话并用新密码更改旧密码。更加简单并且有效。

感谢您的所有帮助。