[英]Accssing ASP.NET 5 / MVC6 Identity Custom Profile data properties
我使用asp.net 5 Web應用程序模板(Mvc6 / MVC core / Asp.net-5)制作了一個名為ShoppingList的示例Web應用程序。 我想使用自定義字段名稱DefaultListId擴展用戶配置文件。
ApplicationUser.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沒有給出與“ UserManager.UserManager(IUserStore,IOptions,IPasswordHasher,IEnumerable>,IEnumerable>,ILookupNormalizer,IdentityErrorDescriber,IServiceProvider,IServiceProvider,ILogProvider)的必需形式參數'optionsAccessor”相對應的參數。 >,IHttpContextAccessor)'ShoppingList.DNX 4.5.1,ShoppingList.DNX Core 5.0 C:\\ Users \\ OleKristian \\ Documents \\ Programmering \\ ShoppingList \\ src \\ ShoppingList \\ Controllers \\ HomeController.cs 15有效
和...
嚴重性代碼說明項目文件行抑制狀態錯誤CS1061'UserManager'不包含'FindById'的定義,並且找不到擴展方法'FindById'接受類型為'UserManager'的第一個參數(您是否缺少using指令或程序集參考?)ShoppingList.DNX 4.5.1,ShoppingList.DNX Core 5.0 C:\\ Users \\ OleKristian \\ Documents \\ Programmering \\ ShoppingList \\ src \\ ShoppingList \\ Controllers \\ HomeController.cs 20有效
您不應UserManager
實例化自己的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
實體實際上已正確存儲在數據庫中。 我們差不多完成了,但是我們現在只需要告訴Entity Framework這個數據庫上下文。 因此,再次在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"]));
就是這樣! 現在,Entity Framework知道了新的用戶實體並將其正確映射到數據庫(包括您的新屬性),而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();
}
如您所見,不需要太多更改即可使該方法異步。 大多數都保持不變。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.