简体   繁体   中英

User.Identity.GetUserId() returns null

When I use User.Identity.GetUserId() method, it returns null. I am trying to make a method (based on AccountController of MVC) to change user's password. The problem is with User.Identity.GetUserId() returning null.

Look my controller and help if you can.

Begin and constructor

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));
    }

Login method

[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");
        }
    }

The manage method that is not working

[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);
    }

I found a way that works for me. I get the ID of user using session, but I discovered that method ChangePasswordAsync() tries to create a new table on database. In my case, I already have a database. So I just create a method that receives session with user ID and change old password by the new password. Much more simple and it works.

Thank you for all help.

我知道您已经接受了答案,但是几个月前我遇到了这个问题,事实证明,我需要用最新版本的MVC来做一些不同的事情。

var id = WebSecurity.GetUserId(User.Identity.Name);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM