[英]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.