1

I am using .net core and I want to access current logged in User property in Inside a Razor page (ExternalLogin.CS.html) and based on user property I want to redirect to different pages.But I am unable to access the user inside the razor page.

public async Task<IActionResult> OnGetCallbackAsync(string returnUrl = null, string remoteError = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (remoteError != null)
            {
                ErrorMessage = $"Error from external provider: {remoteError}";
                return RedirectToPage("./Login", new {ReturnUrl = returnUrl });
            }
            var info = await _signInManager.GetExternalLoginInfoAsync();
            if (info == null)
            {
                ErrorMessage = "Error loading external login information.";
                return RedirectToPage("./Login", new { ReturnUrl = returnUrl });
            }

            // Sign in the user with this external login provider if the user already has a login.
            var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent:false, bypassTwoFactor : true);

            if (result.Succeeded)
            {
                //if (user.verifiedPhone)
                //{
                //    return LocalRedirect("/Identity/Account/Manage");
                //}


                _logger.LogInformation("{Name} logged in with {LoginProvider} provider.", info.Principal.Identity.Name, info.LoginProvider);
                return LocalRedirect("/Customer/ProjectsApiKey/");
            }

            if (result.IsLockedOut)
            {
                return RedirectToPage("./Lockout");
            }

I have tired some answers that are answered for same question but in .net core Move .it's not worked for my Razor pages.

var user = _userManager.GetUser(HttpContext.User);

please give any Suggestions to access current loged-In User inside a Razor

  • https://stackoverflow.com/questions/30701006/how-to-get-the-current-logged-in-user-id-in-asp-net-core – Salah Akbari Oct 09 '19 at 08:50
  • Dear @SalahAkbari , I tested with link you mentioned and still I get null for even for UserID , [link](https://stackoverflow.com/a/52135130/12162568) ,I changed the launchSettings and tested as well . if your are free , Can you please check this link and can you please let me know , is that suitable for my case, I used other class method in the answer.I am going to access userLoggedin inside the Razor page ?(I thought razor page can be seen as other class = not a controller) – Dulith chinthaka Oct 11 '19 at 05:27

0 Answers0