简体   繁体   English

User.Identity.GetUserId()返回null

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

When I use User.Identity.GetUserId() method, it returns null. 当我使用User.Identity.GetUserId()方法时,它返回null。 I am trying to make a method (based on AccountController of MVC) to change user's password. 我试图做一个方法(基于MVC的AccountController )来更改用户的密码。 The problem is with User.Identity.GetUserId() returning null. 问题在于User.Identity.GetUserId()返回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. 我获得了使用会话的用户的ID,但是我发现方法ChangePasswordAsync()尝试在数据库上创建一个新表。 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. 因此,我只创建了一种接收具有用户ID的会话并通过新密码更改旧密码的方法。 Much more simple and it works. 更简单,它可以工作。

Thank you for all help. 感谢您的所有帮助。

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

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

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

相关问题 User.Identity.GetUserId() 登录成功后返回 null - User.Identity.GetUserId() returns null after successful login User.Identity.GetUserId() 在 .net Core 中总是返回 null - User.Identity.GetUserId() always returns null in .net Core User.Identity.GetUserId()在cotrollers构造函数中返回null - User.Identity.GetUserId() returns null in cotrollers constructor 使用User.Identity.GetUserId的ASP MVC应用程序用户为空 - ASP MVC Application User Null using User.Identity.GetUserId User.Identity.GetUserId() 可以被用户伪造吗? - Can User.Identity.GetUserId() be faked by a user? 在SQL查询中获取User.Identity.GetUserId - Get User.Identity.GetUserId in SQL query 在Identity Custom User之后,User.Identity.GetUserId()将错误的类型作为字符串返回 - After Identity Custom User , User.Identity.GetUserId() returns wrong type as string OWIN-在Web API控制器中获取dbo.AspNetUser Id。 User.Identity.GetUserId()为null - OWIN-Getting dbo.AspNetUser Id in Web API controller. User.Identity.GetUserId() is null user.identity.getuserid()返回Web API中的用户ID值 - user.identity.getuserid() return user id value in web api User.Identity.GetUserId()&amp;&amp; User.IsInRole(“角色名称”)不起作用 - User.Identity.GetUserId() && User.IsInRole(“rolename”) Not Working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM