ASP.NET Core 6 MVC 中的身份

问题描述 投票:0回答:1

我正在开发一个使用 ASP.NET Core 标识的 ASP.NET Core MVC 6 应用程序,并且我面临着一个问题,试图找出如何使用 UserManager 类。

但是UserManager总是有空值,我不知道为什么? ASP.NET Core 6 在控制器中没有身份吗?身份支架项目在 Razor 页面中吗? 但我不想使用 Razor 页面

这是代码:

public class AccountController : Controller
{
    private ApplicationSignInManager _signInManager;
    private ApplicationUserManager _userManager;

    public BaseController(ApplicationUserManager userManager, ApplicationSignInManager signInManager)
    {
        UserManager = userManager;
        SignInManager = signInManager;
    }

    public ApplicationSignInManager SignInManager
    {
       get
       {
           return _signInManager;
       }
       private set
       {
          _signInManager = value;
       }
   }

   public ApplicationUserManager UserManager
   {
       get
       {
             return _userManager;
       }
       private set
       {
             _userManager = value;
       }
  }


  [HttpPost]
  [AllowAnonymous]
  [ValidateAntiForgeryToken]
  public async Task<ActionResult> Register(RegisterVM model)
  {
      if (ModelState.IsValid)
      {
          var user = new User
          {
              UserName = model.Email,
              Email = model.Email,
              EmailConfirmed = true
          };

          var result = await UserManager.CreateAsync(user, model.Password);

          if (result.Succeeded)
          {
              await SignInManager.SignInAsync(user, isPersistent: false);
              return RedirectToAction("Index", "Home");
          }
          foreach (var error in result.Errors)
          {
              ModelState.AddModelError(string.Empty, error.Description);
          }
      }

    
      return View(model);
  }
}

ApplicationUserManager类代码:

public sealed class ApplicationUserManager : UserManager<IdentityUser>
{}
c# asp.net-core entity-framework-core asp.net-core-mvc
1个回答
0
投票

如果您从具有“个人”身份验证的 MVC 模板开始,只需注入“UserManager”和“SignInManager”,如下所示:

    public class AccountController : Controller
    {
        private readonly UserManager<IdentityUser> _userManager;
        private readonly SignInManager<IdentityUser> _signInManager;

        public AccountController(UserManager<IdentityUser> userManager,SignInManager<IdentityUser> signInManager)
        {
            this._userManager = userManager;
            this._signInManager = signInManager;
        }

        public async Task<ActionResult> Register(RegisterVM model)
        {
            ...
            var result = await _userManager.CreateAsync(user, model.Password);
            ...
                await _signInManager.SignInAsync(user, isPersistent: false);
        }

如果你想实现自定义 Manager ,那么它会像:

    public class ApplicationUserManager : UserManager<IdentityUser>
    {
        public ApplicationUserManager(IUserStore<IdentityUser> store, IOptions<IdentityOptions> optionsAccessor, IPasswordHasher<IdentityUser> passwordHasher, IEnumerable<IUserValidator<IdentityUser>> userValidators, IEnumerable<IPasswordValidator<IdentityUser>> passwordValidators, ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors, IServiceProvider services, ILogger<UserManager<IdentityUser>> logger) : base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger)
        {
        }
    }
    public class ApplicationSignInManager : SignInManager<IdentityUser>
    {
        public ApplicationSignInManager(UserManager<IdentityUser> userManager, IHttpContextAccessor contextAccessor, IUserClaimsPrincipalFactory<IdentityUser> claimsFactory, IOptions<IdentityOptions> optionsAccessor, ILogger<SignInManager<IdentityUser>> logger, IAuthenticationSchemeProvider schemes, IUserConfirmation<IdentityUser> confirmation) : base(userManager, contextAccessor, claimsFactory, optionsAccessor, logger, schemes, confirmation)
        {
        }
    }

程序.cs

builder.Services.AddDefaultIdentity<IdentityUser>()
    .AddUserManager<ApplicationUserManager>()
    .AddSignInManager<ApplicationSignInManager>()
    .AddEntityFrameworkStores<ApplicationDbContext>();

控制器

    public class AccountController : Controller
    {
        private readonly ApplicationUserManager _userManager;
        private readonly ApplicationSignInManager  _signInManager;

        public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager)
        {
            this._userManager = userManager;
            this._signInManager = signInManager;
        }
© www.soinside.com 2019 - 2024. All rights reserved.