如何检索.NET Core中当前登录用户的电子邮件?

I've gone through many SO threads about this and I've found a way that works (@UserManager.FindByNameAsync(UserManager.GetUserName(User)).Result.Email) however, I need the email to be assigned to a variable within the code for the page so that I can use it for something else and the class for this isn't a controller class so i'm looking to do this outside the controller. My current code for this looks like this:

public class PrescriptionModel : PageModel
    {
        public string userEmail { get; set; }
        private readonly UserManager<ApplicationUser> _userManager;
        public PrescriptionModel(UserManager<ApplicationUser> manager)
        {
            _userManager = manager;
        }

        private async Task<ApplicationUser> GetCurrentUser()
        {
            return await _userManager.GetUserAsync(HttpContext.User);
        }

        public async Task DemoMethod()
        {
            var user = await GetCurrentUser();
            userEmail = user.Email; 
        }
    }

与之对应的HTML是:

<li class="email">
                <label asp-for="userEmail"></label>
                <input asp-for="userEmail" class="form-control" disabled />
</li>

但是,当我进入网站时,我得到的只是一个空白字段。

评论