【问题标题】:where to configure applicationUserManager and role manager in aspnet Identity在 aspnet Identity 中配置 applicationUserManager 和 role manager 的位置
【发布时间】:2015-10-28 21:45:10
【问题描述】:

首先,我使用 AspNet Identity Sample -pre 通过包管理器控制台安装,但 razor 文件不匹配,因此智能感知停止在视图页面上工作。

所以,我选择一步一步手动放置所有代码文件,并在遇到它们时解决了丢失的问题。 所以,我的 App_Start 文件夹中有一个 identityConfig.cs 文件,如下所示----

   public class IdentityConfig
   {
    public class ApplicationUserManager : UserManager<ApplicationUser>
     {
        public ApplicationUserManager(IUserStore<ApplicationUser> store)
            : base(store)
        {
        }
       public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options,
            IOwinContext context)
        {
            var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
            // Configure validation logic for usernames
            manager.UserValidator = new UserValidator<ApplicationUser>(manager)
            {
                AllowOnlyAlphanumericUserNames = false,
                RequireUniqueEmail = true
            };

在我的帐户控制器中,我有这样的代码-------

   public class AccountController : Controller
{
    public AccountController()
    {
    }

    public AccountController(IdentityConfig.ApplicationUserManager userManager, IdentityConfig.ApplicationSignInManager signInManager)
    {
        UserManager = userManager;
        SignInManager = signInManager;
    }

    private IdentityConfig.ApplicationUserManager _userManager;
    public IdentityConfig.ApplicationUserManager UserManager
    {
        get
        {
            return _userManager ?? HttpContext.GetOwinContext().GetUserManager<IdentityConfig.ApplicationUserManager>();
        }
        private set
        {
            _userManager = value;
        }
    }

现在,我在注册操作方法中遇到错误-----

      public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser
            {
                UserName = model.Email,
                Email = model.Email
            };

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

            //  await UserManager.SetTwoFactorEnabledAsync(user.Id, true);

            if (result.Succeeded)
            {
                return await GenerateEmailConfirmation(user);
            }
            AddErrors(result);
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

在这里,如您所见,在第六行,它在第 1 行给出 System.NullReferenceException --------

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

我试图调试它,在我的本地窗口中,它正在传递正确填充数据库所需的所有内容,但只有这个结果变量是 null.also, _signInManager、userManager 和 SignInManager 和 USerManager 在本地窗口中都显示为 null。

有什么问题。我错过了什么。请告诉我。这个 UserMAnager 类有一些关系。我不知道是什么。

【问题讨论】:

    标签: asp.net-mvc controller ef-code-first asp.net-identity


    【解决方案1】:

    确保您在启动类中创建了它们:

    public partial class Startup
    {
        public void ConfigureAuth(IAppBuilder app)
        {
            // Configure the db context, user manager and signin manager to use a single instance per request
            app.CreatePerOwinContext(ApplicationDbContext.Create);
            app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
            app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
    
            ...
        }
    }
    

    【讨论】:

    • Thnxx 的帮助,它的工作就像一个魅力 thnkk 你非常非常非常。请解释我为什么需要在这里配置它。在 aspnet Identity Sample 中,不需要像这样配置它。
    • 返回_userManager ?? HttpContext.GetOwinContext().GetUserManager(); ...您正在从 OwinContext 中获取用户管理器,因此在启动应用程序时,您必须确保它会在其中...
    • 我在以下问题中遇到问题可以看看这个并建议我应该怎么做stackoverflow.com/questions/33455346/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    • 1970-01-01
    • 2018-02-14
    • 2018-06-11
    • 1970-01-01
    • 2017-07-22
    • 1970-01-01
    相关资源
    最近更新 更多