0

I using ASP.NET Identity and when a user want to register get this error:

Value cannot be null.\r\nParameter name: manager.

Here is my Register Action code:

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 });
        }
    }

And the error in this Line: var adminresult = UserManager.Create(user, ps);

ssilas777
  • 9,672
  • 4
  • 45
  • 68
OMID
  • 2,469
  • 4
  • 16
  • 26
  • No UserManager is not null. Yeah I not doing anything with 'ex' but know the error message! Is the SecurityStamp OK? I don't know what is Parameter name manager – OMID Apr 24 '16 at 08:15
  • inner exception is empty – OMID Apr 24 '16 at 08:18
  • Check the 'ex' inner exception and call stack... or add another catch(ArgumentNullException e) and check StackTrace – Mate Apr 24 '16 at 08:21
  • read : http://stackoverflow.com/questions/27060662/usermanager-keeps-throwing-a-system-argumentnullexception AND http://stackoverflow.com/questions/21918000/mvc5-vs2012-identity-createidentityasync-value-cannot-be-null – Mate Apr 24 '16 at 08:24
  • Inner Exception in second catch is null too and StackTrace is this: StackTrace = "at Microsoft.AspNet.Identity.UserManagerExtensions.Create[TUser,TKey](UserManager`2 manager, TUser user, String password)\r\n at Diaxin.Controllers.DefaultController.Register(String nm, String us, String ps) in "Controller Path":line 213" – OMID Apr 24 '16 at 08:34
  • Ok, check 1st link for OWIN or 2nd for SecurityStamp – Mate Apr 24 '16 at 08:37
  • I did all of that but the error is still Alive :| :( – OMID Apr 24 '16 at 10:24

1 Answers1

0

You are getting the error because the role manager was not initialized

To resolve this, follow the steps below

  1. In the IdentityModels.cs file of the Models folder, Add the class below to the Models namespace.

      public class ApplicationRole : IdentityRole
      {
            public ApplicationRole() : base() { }
            public ApplicationRole(string name) : base(name) { }
    
      }
    
  2. In you IdentityConfig.cs file in App_Start folder, Add the class below

    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. Go to Startup.Auth.cs file in the App_Start folder and add the code below to the ConfigureAuth Method.

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

You should stop getting the errors after this.

Hope this helps.

Ahmad Tijani
  • 392
  • 3
  • 10