【发布时间】:2023-03-16 11:28:01
【问题描述】:
我有一个 ASP.NET MVC Core n 层应用程序。我将默认主键更改为整数。没有问题。但我尝试 GetUserId() UserManager 的默认方法返回字符串。是我自己写的方法还是我做错了什么?
//In controller
public int GetLoggedUserId()
{
//it's return still string and of course i can't compile my code
//problem is here
return UserService.GetUserId();
}
public class ApplicationUser : IdentityUser<int>
{
[MaxLength(255)]
public string LogoPath { get; set; }
}
public partial class MyUserManager : UserManager<ApplicationUser>
{
private readonly IUnitOfWork _unitOfWork;
public MyUserManager(IUnitOfWork unitOfWork,
IUserStore<ApplicationUser> store,
IOptions<IdentityOptions> optionsAccessor,
IPasswordHasher<ApplicationUser> passwordHasher,
IEnumerable<IUserValidator<ApplicationUser>> userValidators,
IEnumerable<IPasswordValidator<ApplicationUser>> passwordValidators,
ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors,
IServiceProvider services,
ILogger<UserManager<ApplicationUser>> logger) :
base(store, optionsAccessor, passwordHasher,
userValidators,
passwordValidators,
keyNormalizer,
errors,
services,
logger)
{
_unitOfWork = unitOfWork;
}
}
services.AddDefaultIdentity<ApplicationUser>()
.AddEntityFrameworkStores<MyDbContext>()
.AddUserManager<MyUserManager>()
.AddDefaultTokenProviders();
【问题讨论】:
标签: c# asp.net-mvc asp.net-core dependency-injection asp.net-identity