0

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 = "canEditUser@gamestore.com",
                Email = "canEditUser@gamestore.com",
            };
            **IdUserResult = userMgr.Create(appUser, 
           ConfigurationManager.AppSettings["AppUserPasswordKey"]);**

           
          if(!userMgr.IsInRole(userMgr.FindByEmail("
               canEditUser@gamestore.com").Id, "canEdit"))
            {
                IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("
                 canEditUser@gamestore.com").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>
halfer
  • 19,824
  • 17
  • 99
  • 186
Oifoif
  • 13
  • 5
  • 1
    well its saying that you are passing nulll as the password. I assume that the configuration file you are referring to does not contain one – pm100 Apr 24 '22 at 16:54
  • Please post code as text and not as Image. Other than that: make sure your configuration file does indeed contain a value named `AppUserPasswordKey` – derpirscher Apr 24 '22 at 16:54
  • 1
    Might you please [edit] your question to include code and traceback as **text** rather than as a screenshot? On stack overflow images should not be used for textual content in posts, see [*Discourage screenshots of code and/or errors*](https://meta.stackoverflow.com/a/307500) and [*Why not upload images of code on SO when asking a question*](https://meta.stackoverflow.com/a/285557) for why. For instructions on formatting see *[How do I format my code blocks?](https://meta.stackexchange.com/q/22186)*. – dbc Apr 24 '22 at 16:57
  • A [mcve] showing exactly how to reproduce the problem would maximize your chance of getting help. For why, see [ask]. Absent that your question is likely to be closed as a duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/q/4660142). – dbc Apr 24 '22 at 16:57
  • Hi guys I added the code and removed the image, sorry about that. I checked the config file as you see but there is no AppUserPasswordKey ... – Oifoif Apr 24 '22 at 17:15
  • *The bolded line below* - lines of code in a markdown code block cannot be bolded using `**`; use `` tags instead, and use `
    ` to create the code block
    – Caius Jard Apr 24 '22 at 17:44
  • *the config file for this Admin folder, but there is no password* - and C#'s complaint is that the password is null. Perhaps you're overlooking the part where `ConfigurationManager.AppSettings["AppUserPasswordKey"]` doesn't explode if you mention a setting that doesn't exist - it just returns null, ergo you're providing null by virtue of the fact of not having provided a configured value in the config. Provide a value in the config, or use another approach. Consider storing passwords in user secrets config, not the main one – Caius Jard Apr 24 '22 at 17:51
  • Thank you halfer for trimming my question, I now know how I should ask a "proper" question next time. Thank you pm100 and Caius for your answers, and thank you everyone else. All you said makes sense, and I'll find out where [AppUserPasswordKey] must get the value from. I'll keep trying, and see what I can do about it. Much appreciated! – Oifoif Apr 25 '22 at 05:46

1 Answers1

1

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?

pm100
  • 48,078
  • 23
  • 82
  • 145