【问题标题】:Identity error: Value cannot be null.\r\nParameter name: manager标识错误: 值不能为空。\r\n参数名称: manager
【发布时间】:2016-04-24 07:52:45
【问题描述】:

我使用 ASP.NET Identity 并且当用户想要注册时收到此错误:

值不能为空。\r\n参数名称:经理。

这是我的注册操作代码:

public virtual ActionResult Register(string nm, string em, string ps)
    {
        var redirectUrlPanel = new UrlHelper(Request.RequestContext).Action("Index", "HomeUsers", new { area = "Users" });
        var redirectUrlAuction = new UrlHelper(Request.RequestContext).Action("Auction", "Default", new { area = "" });

        if (ModelState.IsValid)
        {
            try
            {
                var user = new Q_Users();
                user.UserName = nm;
                user.Email = em;
                user.SecurityStamp = Guid.NewGuid().ToString();

                var adminresult = UserManager.Create(user, ps);

                //Add User Admin to Role Admin
                if (adminresult.Succeeded)
                {
                    //Find Role Admin
                    var role = RoleManager.FindByName("Admin");
                    var result = UserManager.AddToRole(user.Id, role.Name);
                    if (result.Succeeded)
                    {
                        return Json(new { OK = "1", UrlPanel = redirectUrlPanel, UrlAuction = redirectUrlAuction });
                    }
                }
                else
                {
                    return Json(new { OK = "0", UrlPanel = redirectUrlPanel, UrlAuction = redirectUrlAuction });
                }
            }
            catch (Exception ex) { }
            return Json(new { OK = "0", UrlPanel = redirectUrlPanel, UrlAuction = redirectUrlAuction });
        }
        else
        {
            return Json(new { OK = "0", UrlPanel = redirectUrlPanel, UrlAuction = redirectUrlAuction });
        }
    }

而这一行的错误:var adminresult = UserManager.Create(user, ps);

【问题讨论】:

  • No UserManager 不为空。是的,我没有对“前”做任何事情,但知道错误信息! SecurityStamp 可以吗?我不知道什么是参数名称管理器
  • 内部异常为空
  • 检查 'ex' 内部异常并调用堆栈...或添加另一个 catch(ArgumentNullException e) 并检查 StackTrace
  • 第二次捕获的内部异常也是空的,StackTrace 是这样的: StackTrace = "在 Microsoft.AspNet.Identity.UserManagerExtensions.Create[TUser,TKey](UserManager`2 manager, TUser user, String password )\r\n 在 Diaxin.Controllers.DefaultController.Register(String nm, String us, String ps) in "Controller Path":line 213"

标签: asp.net-mvc asp.net-identity identity


【解决方案1】:

您收到错误是因为角色管理器未初始化

要解决此问题,请按照以下步骤操作

  1. Models文件夹的IdentityModels.cs文件中,将下面的类添加到Modelsnamespace

      public class ApplicationRole : IdentityRole
      {
            public ApplicationRole() : base() { }
            public ApplicationRole(string name) : base(name) { }
    
      }
    
  2. 在你IdentityConfig.cs文件夹下的IdentityConfig.cs文件中,添加下面的类

    public class ApplicationRoleManager : RoleManager<ApplicationRole>
    {
        public ApplicationRoleManager(IRoleStore<ApplicationRole, string> roleStore)
            : base(roleStore)
        {
        }
    
        public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
        {
            return new ApplicationRoleManager(new RoleStore<ApplicationRole>(context.Get<ApplicationDbContext>()));
        }
    }
    
  3. 转到App_Start 文件夹中的Startup.Auth.cs 文件,并将以下代码添加到ConfigureAuth 方法中。

    app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
    

在此之后您应该停止收到错误。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2021-01-23
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多