[英]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. 看我的控制器,如果可以的话,请帮忙。
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);
}
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.