简体   繁体   English

如何在 .NET Core 中检索当前登录用户的 email?

[英]How can I retrieve the email of the currently logged in user in .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.我已经经历了许多关于此的 SO 线程,并且我找到了一种有效的方法(@UserManager.FindByNameAsync(UserManager.GetUserName(User)).Result.Email)但是,我需要将 email 分配给其中的一个变量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.

I'm getting an error that Object reference not set to an instance of an object.我收到一个错误,即Object reference not set to an instance of an object. on the Email address: @user.Email line within the View when I run it and when changing the return type of the Index() function in HomeController to PrescriptionModel I get the error There is no argument given that corresponds to the required formal parameter 'logger' of PrescriptionModel.PrescriptionModel(ILogger<PrescriptionModel>Email address: @user.Email在视图中的行上,当我运行它并更改 Index() function 中的返回类型时,我得到There is no argument given that corresponds to the required formal parameter 'logger' of PrescriptionModel.PrescriptionModel(ILogger<PrescriptionModel>的错误There is no argument given that corresponds to the required formal parameter 'logger' of PrescriptionModel.PrescriptionModel(ILogger<PrescriptionModel>

Use the model to retrieve any data looks very bad in case of MVC pattern.在 MVC 模式的情况下,使用 model 检索任何看起来非常糟糕的数据。 A better way, if you don't want to touch controller - use code blocks in your view.更好的方法是,如果您不想触摸 controller - 在您的视图中使用代码块。 for example:例如:

@using Microsoft.AspNetCore.Identity

@inject SignInManager<IdentityUser> SignInManager
@inject UserManager<IdentityUser> UserManager
@{
    var usr = await UserManager.GetUserAsync(User);
    var email = usr?.Email;
}

After you can render variable in your view在您可以在视图中呈现变量之后

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

The best way to get data to view is to assign it in controller and pass to the view in model.获取数据以查看的最佳方法是将其分配到 controller 并传递到 model 中的视图。

If this way is not suitable for you - please explain and we can found right solution.如果这种方式不适合您 - 请解释一下,我们可以找到正确的解决方案。

The best way, and how MVC implementation must work: All data came from controller with use of strongly typed Views:最好的方法,以及 MVC 实现必须如何工作:所有数据都来自 controller 使用强类型视图:

 public class HomeController : Controller
    {
        private readonly UserManager<IdentityUser> _userManager;
        public HomeController (UserManager<IdentityUser> manager)
        {
            _userManager = manager;
        }
        public async Task<IActionResult> Index()
        {
            var usr =  await _userManager.GetUserAsync(HttpContext.User);
            return View(new UserModel { Email = usr?.Email });
        }

Where model is:其中 model 是:

public class UserModel
    {
        public string Email { get; set; }
    }

So you just query all information that you need in the controller action/index and pass it to view with your model.因此,您只需在 controller 操作/索引中查询您需要的所有信息,并将其传递给您的 model 进行查看。 This allow you to have different purpose files.这允许您拥有不同用途的文件。 Controller for data manipulatuion/businesss logic Model just DTO to pass values. Controller 用于数据操作/业务逻辑 Model 只是 DTO 来传递值。 View to render it.查看以呈现它。 Check this to have more detailed information: https://docs.microsoft.com/en-us/aspnet/core/mvc/overview?view=aspnetcore-3.1检查此以获得更多详细信息: https://docs.microsoft.com/en-us/aspnet/core/mvc/overview?view=aspnetcore-3.1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在使用Forms的内置用户登录的ASP.NET MVC 3中,如何检索当前登录用户的列表? - In ASP.NET MVC 3 with built-in user login using Forms, how can I retrieve a list of currently logged in users? 如何使用 UserManager 按 ID、名称、email 或其他搜索条件获取用户(不是当前登录的用户)? - How can I get a user (not the currently logged in user) by Id, name, email or other search criteria, using UserManager? ASP.NET Core-如何获取当前登录的用户 - ASP.NET Core - How to get currently logged in user 如何获取ASP.NET Core 2中当前登录用户的用户名,而不是应用程序池详细信息 - How to I get the username of the currently logged in user in ASP.NET Core 2, not Application Pool details 如何找到当前登录的用户 id.Net Core - How can i find the current user id logged in .Net Core 如何使用.Net中当前登录的用户安装服务? - How to install a service with the currently logged user in .Net? 如何将当前登录用户的电子邮件地址获取到 ASP.NET c# 中的视图? - How to get currently logged user's email address to the View in ASP.NET c#? 如何在ASP.NET Web应用程序中检索“登录用户”? - How can I retrieve Logged In user in an ASP.NET web application? 如何将已登录的用户重定向到 Asp.Net Core web 应用程序中的索引(剃须刀页面) - How can I redirect an already logged user to Index in Asp .Net Core web Application (razor pages) 如何将用户字段设置为当前记录的用户ASP.NET MVC的数据库记录添加 - How should I add a database record with user field set to currently logged user ASP.NET MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM