0

I'm new at Neo4j and I'm going to use neo4j.AspNet.Identity for my authentication and authorization. I'm using neo4jUserManager and also neo4jUserStore.But when I run the application, in Neo4jUserManager create method I'll face with NullExceptionReference and I don't know why? Can Anybody help me? Below is my code

 public class Neo4jUserManager : UserManager<ApplicationUser>
{
    public Neo4jUserManager(IUserStore<ApplicationUser> store)
        : base(store)
    {
    }

    public async Task SetLastLogin()
    {
        //            Store.FindByIdAsync()
    }

    public static Neo4jUserManager Create(IdentityFactoryOptions<Neo4jUserManager> options, IOwinContext context)
    {
        var graphClientWrapper = context.Get<GraphClientWrapper>();
        var manager = new Neo4jUserManager(new Neo4jUserStore<ApplicationUser>(graphClientWrapper.GraphClient));

        // Configure validation logic for usernames
        //            manager.UserValidator = new UserValidator<Neo4jUser>(manager)
        //            {
        //                AllowOnlyAlphanumericUserNames = false,
        //                RequireUniqueEmail = true
        //            };

        manager.PasswordValidator = new PasswordValidator
        {
            RequiredLength = 6,
            RequireNonLetterOrDigit = true,
            RequireDigit = true,
            RequireLowercase = true,
            RequireUppercase = true
        };

        // Configure user lockout defaults
        manager.UserLockoutEnabledByDefault = false;
        manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
        manager.MaxFailedAccessAttemptsBeforeLockout = 5;

        // Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
        // You can write your own provider and plug it in here.
        //            manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<Neo4jUser>
        //            {
        //                MessageFormat = "Your security code is {0}"
        //            });
        //            manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<Neo4jUser>
        //            {
        //                Subject = "Security Code",
        //                BodyFormat = "Your security code is {0}"
        //            });
        //            manager.EmailService = new EmailService();
        //            manager.SmsService = new SmsService();
        var dataProtectionProvider = options.DataProtectionProvider;
        if (dataProtectionProvider != null)
        {
            manager.UserTokenProvider =
                new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
        }
        return manager;
    }
}
Mehran Ahmadi
  • 311
  • 1
  • 3
  • 10
  • Firstly - What is the stacktrace of your error. Secondly - How do you setup the `GraphClientWrapper`. If you're getting a `NullReferenceException`, the something is `null`. You should easily be able to find this out. As you're using the version of `Neo4j.AspNet.Identity` that contains `Neo4jUserManager` you *have* to be using the source code and not a nuget package - so it should be even easier to debug. – Charlotte Skardon May 20 '15 at 13:58
  • I use source code and reference it to my project, and stack trace message is {"Object reference not set to an instance of an object."} . can u help me if I want to use Neo4jUserManager what should I do? I mean I don't know how to setup `GraphClientWrapper` @ChrisSkardon – Mehran Ahmadi May 20 '15 at 14:12
  • No, that's *not* a stack trace, that's the message. What is the stack trace, where *exactly* is the error thrown. I'm guessing as you don't know how to Setup `GraphClientWrapper` that your `graphClientWrapper` var is null? – Charlotte Skardon May 20 '15 at 14:25
  • Yeah I don't know how to setup `GraphClientWrapper` and so `graphClientWrapper` is null,can you help me how to setup `GraphClientWrapper`? @ChrisSkardon – Mehran Ahmadi May 20 '15 at 14:30
  • Thanks alot beacuse of your guidance,I undrestand somewhat but in `ConfigureAuth.cs` file I should add this line of code `app.CreatePerOwinContext(GraphClientWrapper.create); `and also this line `app.CreatePerOwinContext(Neo4jUserManager.Create);` but `GraphClient` doesn't have any create method should I change the code and rebuild the dll OR I am in a wrong way? Can you help me one more time? @ChrisSkardon – Mehran Ahmadi May 20 '15 at 20:56

1 Answers1

1

To setup the graphClientWrapper, you can do this:

var gc = new GraphClient(new Uri("http://localhost.:7474/db/data"));
gc.Connect();
var graphClientWrapper = new GraphClientWrapper(gc);
var manager = new Neo4jUserManager(new Neo4jUserStore<ApplicationUser>(graphClientWrapper.GraphClient));

Also, as a side I would read up on how to use Owin. It should have been obvious to you that you're trying to pull from an IOwinContext object, and that if you're getting null you should investigate how to set that up. I imagine less than 5 minutes on Google would have helped you. OR just looking at what calls that method would have shown you how the EntityFramework was being setup in the template you're modifying.

Charlotte Skardon
  • 6,220
  • 2
  • 31
  • 42
  • Thanks alot beacuse of your guidance,I undrestand somewhat but in `ConfigureAuth.cs` file I should add this line of code `app.CreatePerOwinContext(GraphClientWrapper.create);` and also this line `app.CreatePerOwinContext(Neo4jUserManager.Create);` but GraphClient doesn't have any create method should I change the code and rebuild the dll **OR** I am in a wrong way? Can you help me one more time? @ChrisSkardon – Mehran Ahmadi May 20 '15 at 16:46
  • Read up on Owin, I'm not going to explain any more, as I've had 3 people in the last 24 hours ask exactly the same thing (including yourself) directly to my email address, leading me to think this is something like homework. – Charlotte Skardon May 21 '15 at 08:43