繁体   English   中英

User.Identity.GetUserId()返回null

[英]User.Identity.GetUserId() returns null

当我使用User.Identity.GetUserId()方法时,它返回null。 我试图做一个方法(基于MVC的AccountController )来更改用户的密码。 问题在于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));
    }

登录方式

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

无效的管理方法

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

我找到了一种对我有用的方法。 我获得了使用会话的用户的ID,但是我发现方法ChangePasswordAsync()尝试在数据库上创建一个新表。 就我而言,我已经有一个数据库。 因此,我只创建了一种接收具有用户ID的会话并通过新密码更改旧密码的方法。 更简单,它可以工作。

感谢您的所有帮助。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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