Search code examples
c#visual-studioasp.net-identityroles

How to debug this "value cannot be null. Parameter name: password" error?


Basically, I am trying to create an admin role (canEdit) that can add/remove products in the application. Analyzing the "IdentityResult Create" extension method I found out it has +1 override.

  1. One has the parameters: manager, TUser user
  2. The other has parameters: manager, TUser user, string password

This is an error I kept getting towards the end of the Wingtip Toys Microsoft asp.net tutorial. In my code which is identical to the tutorial except for the name of the Application (mine is gamestore as you see), I think I must use the 2nd method as the admin role has a password.

But I have tried different things and to understand and fix this by analyzing the metadata extensions for all those light blue classes and interfaces, etc you see, but I wasn't able to fix this at all.

Edit

I am adding code. The bolded line below is the erroring line.

namespace GameStore.Logic
{
    internal class RoleActions
    {
        internal void AddUserAndRole()
        {
            // Access the application context and create result 
            variables.
            Models.ApplicationDbContext context = new 
            ApplicationDbContext();
            IdentityResult IdRoleResult;
            IdentityResult IdUserResult;

            var roleStore = new RoleStore<IdentityRole>(context);
            var roleMgr = new RoleManager<IdentityRole>(roleStore);

            if (!roleMgr.RoleExists("canEdit"))
            {
                IdRoleResult = roleMgr.Create(new IdentityRole { Name = 
                "canEdit" });
            }
    
            var userMgr = new UserManager<ApplicationUser>(new 
                   UserStore<ApplicationUser>(context));
            var appUser = new ApplicationUser
            {
                UserName = "[email protected]",
                Email = "[email protected]",
            };
            **IdUserResult = userMgr.Create(appUser, 
           ConfigurationManager.AppSettings["AppUserPasswordKey"]);**

           
          if(!userMgr.IsInRole(userMgr.FindByEmail("
               [email protected]").Id, "canEdit"))
            {
                IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("
                 [email protected]").Id, "canEdit");
            }
        }
    }
}

Below is the config file for this Admin folder, but there is no password involved as you see..

<?xml version="1.0"?>
  <configuration>
  <system.web>
     <authorization>
       <allow roles="canEdit"/>
       <deny users="*"/>
      </authorization>
  </system.web>
  </configuration>

Solution

  • Where do you expect

    ConfigurationManager.AppSettings["AppUserPasswordKey"]
    

    to get the password from? You did not put a value in the configuration file for it to read.

    I have to say that it would be very strange to put the passowrd for a new user in the ap config file.

    I mean you say 'see there is no password here', well exactly, so why is your code trying to read a password from there?