Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Get Id of Current User

$
0
0

Hi Devs, 

I´m trying to get the Current User Id so I can make a search in my Users object to find witch user is logged in. I try the following code:

public async Task<IActionResult> login(Login login)
        {
            if (ModelState.IsValid)
            {

                var result = await login_Manager.PasswordSignInAsync(login.Username, login.Password, login.LembrarUsuario, false);
                if (result.Succeeded)
                {
                    var current_User= user_Manager.GetUserAsync(HttpContext.User);
                    string current_User_Id = "" + current_User.Id;

                    User user= _context.Users.FirstOrDefault(x => x.Id == current_User_Id );

                    return View("~/Areas/Module/Views/Home/Index.cshtml");
                }
                else
                {
                    ViewBag.login_Error= "Incorrect username or password";
                    return View("~/Areas/Module/Views/Users/Login.cshtml", login);
                }

            }
            return View("~/Areas/Module/Views/Users/Login.cshtml", login);
        }

I noticed that the value of currer_User_Id is always changing when I run the application and the user variable is always returning null because the id doesn´t match.

Best Regards


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>