1

I've got a simple web application and I can't user User.Identity.GetUserId() method. Every time I use it, it returns back null, or 0 if I use User.Identity.GetUserId<int>().

For example in this code part.

public async Task<ActionResult> SetPassword(SetPasswordViewModel model)
        {
            var userId = User.Identity.GetUserId();
            if (ModelState.IsValid)
            {
                var result = await UserManager.AddPasswordAsync(User.Identity.GetUserId(), model.NewPassword);
                if (result.Succeeded)
                {
                    var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
                    if (user != null)
                    {
                        await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
                    }
                    return RedirectToAction("Index", new { Message = ManageMessageId.SetPasswordSuccess });
                }
                AddErrors(result);
            }

Please , someone help me with this problem. Thanks again.

tereško
  • 58,060
  • 25
  • 98
  • 150
UnuS76
  • 149
  • 1
  • 13
  • have you stepped thru the code..? and if so what is the value of `userId` after you step pass this line `var userId = User.Identity.GetUserId();` also `AddErrors(result);` should't that be wrapped around an `else{}` statement – MethodMan Nov 18 '14 at 22:21
  • What kind of authentication are you using? – Ryan Schlueter Nov 18 '14 at 22:22
  • DJ KRAZE, I've stopped through the code, and as I said , it's gives back 0 or null, depends on what type of value you want it to return. – UnuS76 Nov 18 '14 at 22:24
  • Ryan Schlueter, I'm using the default authentication of the web application of MVC, if you was asking for that. – UnuS76 Nov 18 '14 at 22:26
  • I would take a look at these two posts my guess eithere authentication or iis issue using annonymous http://stackoverflow.com/questions/7839567/why-does-httpcontext-current-user-identity-name-return-blank http://stackoverflow.com/questions/19515890/authentication-issue-when-debugging-in-vs2013-iis-express/ – Ryan Schlueter Nov 18 '14 at 22:35
  • Authentication mode is set to "Forms" – UnuS76 Nov 18 '14 at 22:38
  • I've tried to check whether user is Authenticated or no, but it returned true. – UnuS76 Nov 18 '14 at 22:44
  • Authentication mode "Forms" is the problem. – trailmax Nov 18 '14 at 23:23
  • So what should I do to fix it ? – UnuS76 Nov 18 '14 at 23:26
  • Should be `AuthenticationMode="None"` – jamesSampica Nov 19 '14 at 02:53
  • Check this one http://stackoverflow.com/questions/26494063/securing-cookies-for-default-mvc-5-application/26496321#26496321 - and update your config. – DSR Nov 19 '14 at 10:35
  • @shoe It must be "Form", but even if I change it to "None", nothing happens, and I can't pass the Log in Form. – UnuS76 Nov 19 '14 at 12:25

0 Answers0