ASP.NET 5 / MVC6 标识自定义配置文件数据属性
本文关键字:自定义 配置文件 数据属性 标识 MVC6 NET ASP | 更新日期: 2023-09-27 18:34:26
我使用 asp.net 5 Web 应用程序模板(Mvc6/MVC core/Asp.net-5(制作了一个名为 ShoppingList 的示例 Web 应用程序。我想使用自定义字段名称 DefaultListId 扩展用户配置文件。
应用程序用户.cs:
namespace ShoppingList.Models
{
// Add profile data for application users by adding properties to the ApplicationUser class
public class ApplicationUser : IdentityUser
{
public int DefaultListId { get; set; }
}
}
在家庭控制器中,我想访问为此属性存储的数据。我试过了:
namespace ShoppingList.Controllers
{
public class HomeController : Controller
{
private UserManager<ApplicationUser> userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
public IActionResult Index()
{
var userId = User.GetUserId();
ApplicationUser user = userManager.FindById(userId);
ViewBag.UserId = userId;
ViewBag.DefaultListId = user.DefaultListId;
return View();
}
//other actions omitted for brevity
但是我收到以下错误:
严重性代码说明项目文件行抑制状态 错误 CS7036 没有给出对应于 必需的形式参数 'optionsAccessor' 的 'UserManager.UserManager(IUserStore, IOptions, IPasswordHasher, IEnumerable>, IEnumerable>, ILookupNormalizer, IdentityErrorDescriber, IServiceProvider, 伊罗格>, IHttpContextAccessor(' ShoppingList.DNX 4.5.1, ShoppingList.DNX Core 5.0 C:''用户''OleKristian''文档''Programmering''ShoppingList''src''ShoppingList''Controllers''HomeController.cs 15 Active
和。。。
严重性代码说明项目文件行抑制状态 错误 CS1061"用户管理器"不包含 定义"FindById"且不接受扩展方法"FindById" 可以找到类型为"用户管理器"的第一个参数 (是否缺少 using 指令或程序集 参考? 购物清单.DNX 4.5.1, 购物清单.DNX 核心 5.0 C:''用户''OleKristian''文档''Programmering''ShoppingList''src''ShoppingList''Controllers''HomeController.cs 20 Active
你不应该像以前一样实例化你自己的UserManager
。实际上很难做到这一点,因为它需要你向构造函数传递很多参数(而且其中大多数东西也很难正确设置(。
ASP.NET Core 广泛使用依赖注入,因此您应该以自动接收用户管理器的方式设置控制器。这样,您就不必担心创建用户管理器:
public class HomeController : Controller
{
private readonly UserManager<ApplicationUser> userManager;
public HomeController (UserManager<ApplicationUser> userManager)
{
this.userManager = userManager;
}
// …
}
但是,为此,您首先需要设置 ASP.NET 身份以实际了解您的ApplicationUser
并使其用于存储您的用户身份。为此,您需要修改Startup
类。在 ConfigureServices
方法中,需要更改AddIdentity
调用以使其引用实际类型:
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
此处IdentityRole
是指 ASP.NET Identity 使用的标准角色类型(因为不需要自定义角色类型(。如您所见,我们还引用了一个ApplicationDbContext
,它是修改后的标识模型的实体框架数据库上下文;所以我们也需要设置那个。在您的情况下,它可能看起来像这样:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// here you could adjust the mapping
}
}
这将确保ApplicationUser
实体实际正确存储在数据库中。我们几乎完成了,但我们现在只需要告诉实体框架有关此数据库上下文的信息。因此,同样在 Startup
类的 ConfigureServices
方法中,请确保调整AddEntityFramework
调用以设置ApplicationDbContext
数据库上下文。如果您有其他数据库上下文,则可以仅链接这些上下文:
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<IdentityContext>(opts => opts.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]))
.AddDbContext<DataContext>(opts => opts.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
就是这样!现在,实体框架知道新的用户实体并将其正确映射到数据库(包括您的新属性(,并且 ASP.NET Identity 也知道您的用户模型,并将该用户模型用于它所做的一切,您可以将UserManager
注入控制器(或服务或其他(来执行操作。
至于你的第二个错误,你得到这个是因为用户管理器没有FindById
方法;它只是一个FindByIdAsync
方法。实际上,在很多地方,你会看到 ASP.NET Core只有异步方法,所以接受它并开始使你的方法异步。
在您的情况下,您需要像这样更改Index
方法:
// method is async and returns a Task
public async Task<IActionResult> Index()
{
var userId = User.GetUserId();
// call `FindByIdAsync` and await the result
ApplicationUser user = await userManager.FindByIdAsync(userId);
ViewBag.UserId = userId;
ViewBag.DefaultListId = user.DefaultListId;
return View();
}
如您所见,它不需要很多更改即可使方法异步。其中大部分保持不变。