1

Hello I have published my website which was forking ok locally from visual studio to Azure and now everytime I try to access the page I am getting the following exception.

[NullReferenceException: Object reference not set to an instance of an object.]
   WebApplication1.Logic.RoleActions.AddUserAndRole() +390
   WebApplication1.Global.Application_Start(Object sender, EventArgs e) +47

Because its pointing to Logic.RoleActions and Global.Application_Start I will post code in those.

RoleActions

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

        // Create a RoleStore object by using the ApplicationDbContext object. 
        // The RoleStore is only allowed to contain IdentityRole objects.
        var roleStore = new RoleStore<IdentityRole>(context);

        // Create a RoleManager object that is only allowed to contain IdentityRole objects.
        // When creating the RoleManager object, you pass in (as a parameter) a new RoleStore object. 
        var roleMgr = new RoleManager<IdentityRole>(roleStore);

        // Then, you create the "canEdit" role if it doesn't already exist.
        if (!roleMgr.RoleExists("canEdit"))
        {
            IdRoleResult = roleMgr.Create(new IdentityRole { Name = "canEdit" });
        }

        // Create a UserManager object based on the UserStore object and the ApplicationDbContext  
        // object. Note that you can create new objects and use them as parameters in
        // a single line of code, rather than using multiple lines of code, as you did
        // for the RoleManager object.
        var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
        var appUser = new ApplicationUser
        {
            UserName = "canEditUser@wingtiptoys.com",
            Email = "canEditUser@wingtiptoys.com"
        };
        IdUserResult = userMgr.Create(appUser, "Pa$$word1");

        // If the new "canEdit" user was successfully created, 
        // add the "canEdit" user to the "canEdit" role. 
        if (!userMgr.IsInRole(userMgr.FindByEmail("canEditUser@wingtiptoys.com").Id, "canEdit"))
        {
            IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("canEditUser@wingtiptoys.com").Id, "canEdit");
        }

        if (!userMgr.IsInRole(userMgr.FindByEmail("kasi64@seznam.cz").Id, "canEdit"))
        {
            IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("kasi64@seznam.cz").Id, "canEdit");
        }
    }
}
}

and Global

namespace WebApplication1
{
public class Global : HttpApplication
{
    void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            // Create the custom role and user.
            RoleActions roleActions = new RoleActions();
            roleActions.AddUserAndRole();



        }
    }
}

I dont understand why it works fine locally but when published its a problem. Thank you

Pavel Kašelják
  • 341
  • 3
  • 16
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) –  May 27 '15 at 20:36

2 Answers2

1

The NullReferenceException is most likely caused by one of the userMgr.FindByEmail("xxx") calls. You do not handle the case where the userMgr.FindByEmail method returns null, and you directly access it's Id property.

Bas
  • 25,270
  • 7
  • 45
  • 82
1

I suspect it is because there is no user with an email of kasi64@seznam.cz in Azure. Therefore, calling Id on this line will throw a null reference exception.

userMgr.FindByEmail("kasi64@seznam.cz").Id
Donal
  • 25,862
  • 9
  • 57
  • 70