grandchamp / Identity.Dapper

Identity package that uses Dapper instead EntityFramework for use with .NET Core
MIT License
268 stars 62 forks source link

NullReferenceException: Object reference not set to an instance of an object. #51

Closed eld1887 closed 6 years ago

eld1887 commented 6 years ago

hi, i try to run your example. I use mysql and i created the database identity and the tables with the given sql script. if i try to register a new user i get the follwing error msg:

An unhandled exception occurred while processing the request. NullReferenceException: Object reference not set to an instance of an object.

Microsoft.AspNetCore.Identity.UserClaimsPrincipalFactory+d__5.MoveNext()

NullReferenceException: Object reference not set to an instance of an object.

Microsoft.AspNetCore.Identity.UserClaimsPrincipalFactory+<GenerateClaimsAsync>d__5.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.TaskAwaiter.GetResult()
Microsoft.AspNetCore.Identity.UserClaimsPrincipalFactory+<CreateAsync>d__9.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.TaskAwaiter.GetResult()
Microsoft.AspNetCore.Identity.SignInManager+<CreateUserPrincipalAsync>d__25.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.TaskAwaiter.GetResult()
Microsoft.AspNetCore.Identity.SignInManager+<SignInAsync>d__30.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Identity.Dapper.Samples.Web.Controllers.AccountController+<Register>d__9.MoveNext() in AccountController.cs

                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713
                    // Send an email with this link
                    //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                    //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
                    //await _emailSender.SendEmailAsync(model.Email, "Confirm your account",
                    //    $"Please confirm your account by clicking this link: <a href='{callbackUrl}'>link</a>");

                    await _signInManager.SignInAsync(user, isPersistent: false);

                    _logger.LogInformation(3, "User created a new account with password.");
                    return RedirectToLocal(returnUrl);
                }
                AddErrors(result);
            }

System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.TaskAwaiter.GetResult()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+<InvokeActionMethodAsync>d__12.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+<InvokeNextActionFilterAsync>d__10.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+<InvokeInnerFilterAsync>d__14.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+<InvokeNextResourceFilter>d__22.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+<InvokeFilterPipelineAsync>d__17.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+<InvokeAsync>d__15.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Builder.RouterMiddleware+<Invoke>d__4.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware+<Invoke>d__6.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware+<Invoke>d__7.MoveNext()
`

I only change your Adress field with FirstName and then i get this error.
Thanks for help
grandchamp commented 6 years ago

Can you upload on github the full code?

eld1887 commented 6 years ago

Sorry for the late answer. Here is the code. https://github.com/eld1887/identity

Update: ironically i get the error from above, but the user was inserted into the database...but it seems that´s a problem with login. (i disabled the ssl mode for my database(to find in the connection string))

pic from below shows the errors after i tried to login: http://abload.de/image.php?img=loginerrorsxplke.jpg

I don´t know if the code is wrong or something is wrong with my database. Thanks for your help

grandchamp commented 6 years ago

Probably is something with your database. I had tried here with the same custom fields you added on entities and the script is:

CREATE TABLE `identitylogin` (
  `LoginProvider` VARCHAR(128) NOT NULL,
  `ProviderKey` VARCHAR(128) NOT NULL,
  `UserId` INT NOT NULL,
  `Name` VARCHAR(256) NOT NULL,
  PRIMARY KEY (`ProviderKey`, `UserId`, `LoginProvider`));

CREATE TABLE `identityrole` (
  `Id` INT NOT NULL AUTO_INCREMENT,
  `Name` VARCHAR(50) NOT NULL,
  `IsDummy` BIT(1) NOT NULL,

  PRIMARY KEY (`Id`));

CREATE TABLE `identityuser` (
  `Id` INT NOT NULL AUTO_INCREMENT,
  `Username` VARCHAR(256) NOT NULL,
  `Email` VARCHAR(256) NULL,
  `FirstName` VARCHAR(256) NULL,
  `EmailConfirmed` BIT(1) NOT NULL,
  `PasswordHash` LONGTEXT NULL,
  `SecurityStamp` VARCHAR(38) NULL,
  `PhoneNumber` VARCHAR(50) NULL,
  `PhoneNumberConfirmed` BIT(1) NOT NULL,
  `TwoFactorEnabled` BIT(1) NOT NULL,
  `LockoutEnd` DATETIME NULL,
  `LockoutEnabled` BIT(1) NOT NULL,
  `AccessFailedCount` INT NOT NULL,
  PRIMARY KEY (`Id`));

CREATE TABLE `identityuserclaim` (
  `Id` INT NOT NULL AUTO_INCREMENT,
  `UserId` INT NOT NULL,
  `ClaimType` VARCHAR(256) NOT NULL,
  `ClaimValue` VARCHAR(256) NULL,
  PRIMARY KEY (`Id`));

CREATE TABLE `identityuserrole` (
  `UserId` INT NOT NULL,
  `RoleId` INT NOT NULL,
  PRIMARY KEY (`UserId`, `RoleId`),
  INDEX `FK_IdentityUserRole_IdentityRole_idx` (`RoleId` ASC),
  CONSTRAINT `FK_IdentityUserRole_IdentityRole`
    FOREIGN KEY (`RoleId`)
    REFERENCES `identityrole` (`Id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `FK_IdentityUserRole_IdentityUser`
    FOREIGN KEY (`UserId`)
    REFERENCES `identityuser` (`Id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE);

ALTER TABLE `identitylogin` 
ADD INDEX `FK_IdentityLogin_IdentityUser_idx` (`UserId` ASC);
ALTER TABLE `identitylogin` 
ADD CONSTRAINT `FK_IdentityLogin_IdentityUser`
  FOREIGN KEY (`UserId`)
  REFERENCES `identityuser` (`Id`)
  ON DELETE CASCADE
  ON UPDATE CASCADE;

ALTER TABLE `identityuserclaim` 
ADD INDEX `FK_IdentityUserClaim_IdentityUser_idx` (`UserId` ASC);
ALTER TABLE `identityuserclaim` 
ADD CONSTRAINT `FK_IdentityUserClaim_IdentityUser`
  FOREIGN KEY (`UserId`)
  REFERENCES `identityuser` (`Id`)
  ON DELETE CASCADE
  ON UPDATE CASCADE;
eld1887 commented 6 years ago

Thank you, with the new sql script it works fine. I don´t know where exactly the mistake was, but it runs :D

eld1887 commented 6 years ago

btw i started a new project and when i try to launch it, i get follwing error msg:

edit: solved the error by my own.

Thanks for your help and your hard work.

grandchamp commented 6 years ago

@eld1887 if you are using the CustomRole it has a property called IsDummy, probably on your previous database it wasn't there.