UserManager<T>
クラスは、あなたがそれを自分で定義する必要があります。デフォルトの実装を使用するか、必要に応じて独自のクラスを定義することができます。例えば
:ユーザーが(例えばDB)から来て、どこがClaimsPrincipal
からのいかなる請求から独自のユーザーを取得することができる場所
MyUserStore.cs
です。
:
public class MyUserStore: IUserStore<MyUser>, IQueryableUserStore<MyUser>
{
// critical method to bridge between HttpContext.User and your MyUser class
public async Task<MyUser> FindByIdAsync(string userId, CancellationToken cancellationToken)
{
// that userId comes from the ClaimsPrincipal (HttpContext.User)
var user = _users.Find(userId);
return await Task.FromResult(user);
}
}
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// you'll need both a user and a role class. You can also implement a RoleStore if needed
services
.AddIdentity<MyUser, MyRole>()
.AddUserStore<MyUserStore>();
services.Configure<IdentityOptions>(options =>
{
// This claim will be used as userId in MyUserStore.FindByIdAsync
options.ClaimsIdentity.UserIdClaimType = ClaimTypes.Name;
});
}
MyControllerは.cs
次に、あなたのコントローラでは、あなたはUserManager<MyUser>
クラスにアクセスすることができます210
public class MyController : Controller
{
private readonly UserManager<User> _userManager;
public MyController(UserManager<User> userManager)
{
_userManager = userManager;
}
[HttpGet("whatever")]
public async Task<IActionResult> GetWhatever()
{
// this will get your user from the UserStore,
// based on the ClaimsIdentity.UserIdClaimType from the ClaimsPrincipal
MyUser myUser = await _userManager.GetUserAsync(User);
}
}