我在 mac 计算机上使用 asp.net core,我正在尝试为我的 asp.net mvc Web 应用程序创建一个自定义 ApplicationUser,该应用程序与基本 IdentityUser 配合得很好。
尽管遵循 Microsoft 的本指南:
我遇到这个错误:
{"error":"No service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' has been registered."}
以下是我的代码片段:
startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// [...]
services.AddDbContext<ApplicationDbContext>(
options => options.UseSqlServer(identityDbContextConnection));
// Relevant part: influences the error
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc(config =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
config.Filters.Add(new AuthorizeFilter(policy));
});
}
ApplicationUser.cs
// Add profile data for application users by adding properties to the ApplicationUser class
public class ApplicationUser : IdentityUser
{
[Required]
public string DrivingLicense { get; set; }
}
注册.cshtml.cs
public class RegisterModel : PageModel
{
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly UserManager<ApplicationUser> _userManager;
private readonly ILogger<RegisterModel> _logger;
private readonly IServiceProvider _services;
public RegisterModel(
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
ILogger<RegisterModel> logger,
IServiceProvider services
)
{
_userManager = userManager;
_signInManager = signInManager;
_logger = logger;
_services = services;
}
[BindProperty]
public InputModel Input { get; set; }
public string ReturnUrl { get; set; }
public class InputModel
{
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
// Added for ApplicationUser
[Required]
[Display(Name = "Driving License")]
public string DrivingLicense { get; set; }
// -----------------------------
// [...]
}
public void OnGet(string returnUrl = null)
{
ReturnUrl = returnUrl;
}
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
returnUrl = returnUrl ?? Url.Content("~/");
if (ModelState.IsValid)
{
var user = new ApplicationUser {
UserName = Input.Email,
Email = Input.Email,
DrivingLicense = Input.DrivingLicense // property added by ApplicationUser
};
var result = await _userManager.CreateAsync(user, Input.Password);
if (result.Succeeded)
{
_logger.LogInformation("User created a new account with password.");
await _signInManager.SignInAsync(user, isPersistent: false);
return LocalRedirect(returnUrl);
}
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
}
// If we got this far, something failed, redisplay form
return Page();
}
}
来自Manage/Index.cshtml.cs的片段
public class InputModel
{
[Required]
[EmailAddress]
public string Email { get; set; }
// Added for ApplicationUser
[Required]
[Display(Name = "Driving License")]
public string DrivingLicense { get; set; }
// -----------------------------
[Phone]
[Display(Name = "Phone number")]
public string PhoneNumber { get; set; }
}
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
// [...]
// Added for ApplicationUser
if (Input.DrivingLicense != user.DrivingLicense)
{
user.DrivingLicense = Input.DrivingLicense;
}
await _userManager.UpdateAsync(user);
// -------------------------
await _signInManager.RefreshSignInAsync(user);
StatusMessage = "Your profile has been updated";
return RedirectToPage();
}
ApplicationDbContext
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}
我无法遵循微软官方指南的唯一部分是编辑 Account/Manage/Index.cshtml 的部分,因为当我执行 CLI 步骤时,该文件没有搭建起来!
请注意,当我在 startup.cs 中将 ApplicationUser 替换为 IdentityUser 时,如下所示:
services.AddIdentity<IdentityUser, IdentityRole>()
应用程序打开,但注册当然无法按预期正常工作。
最佳答案
问题出在“_LoginPartial.cshtml”
删除这个
@using Microsoft.AspNetCore.Identity
@inject SignInManager<IdentityUser> SignInManager
@inject UserManager<IdentityUser> UserManager
添加这个
@using Microsoft.AspNetCore.Identity
@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager
关于c# - 类型 'Microsoft.AspNetCore.Identity.UserManager' : while trying to extend IdentityUser? 无服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51679144/