Why does UserManager.FindById() return null if I do not relog?

Simon Verbeke

I'm trying to find the user associated with the currently logged on user:

public ActionResult Root(string path)
{
    var id = User.Identity.GetUserId(); //This works
    var currentUser = manager.FindById(id); //This returns null

    return View(db.Folders.ToList()
        .Where(folder => folder.User.Id == currentUser.Id)
        .Where(folder => folder.Path == path));
}

This only works if I do not use the indicated part in my seed method. If I do execute this part, manager.FindById() returns null.

    protected override void Seed(ApplicationDbContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context", "Context must not be null.");
        }

        const string UserName = "[email protected]";
        const string RoleName = "Admin";

        var userRole = new IdentityRole { Name = RoleName, Id = Guid.NewGuid().ToString() };
        context.Roles.Add(userRole);

        var hasher = new PasswordHasher();

        var user = new ApplicationUser
        {
            UserName = UserName,
            PasswordHash = hasher.HashPassword("123456"),
            Email = "[email protected]",
            EmailConfirmed = true,
            SecurityStamp = Guid.NewGuid().ToString()
        };

        user.Roles.Add(new IdentityUserRole { RoleId = userRole.Id, UserId = user.Id });

        context.Users.Add(user);

        //If I leave this part out, there are no issues.
        new List<Folder>
        { 
            new Folder{Name = "Test", Path = "", User = user},
            new Folder{Name = "Bla", Path = "Test", User = user},
            new Folder{Name = "Lala", Path = "Test/Bla", User = user}
        }.ForEach(f => context.Folders.Add(f));

        context.SaveChanges();

        base.Seed(context);
    }

EDIT: Starting to narrow it down. If I relog my user, everything works just fine. The active user during testing remains logged in from the previous debugging session.

trailmax

I see the problem:

The active user during testing remains logged in from the previous debugging session.

인증 쿠키에는 이전 세션의 사용자 ID에 대한 Guid가 포함됩니다. 그리고 매번 사용자를 다시 생성하면 userId에 대한 guid가 데이터베이스에서 변경되고 쿠키에 저장된 Id와 일치하지 않습니다. 따라서 모든 디버그 세션에서 사용자를 다시 생성하거나 모든 디버그에서 쿠키를 종료하지 마십시오.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Why does SSIS lookup return null value?

분류에서Dev

Why does this MySQL stored function return null?

분류에서Dev

why does new Integer(i).hashCode() return i?

분류에서Dev

Why does my code return wrong sum when i concatenate it?

분류에서Dev

Why does new $class; return null when class_exists($class) returns true?

분류에서Dev

Why does Vim delay on this remapped key? And how do I fix it?

분류에서Dev

Why does ArrayList do this?

분류에서Dev

why nextelementsibling return null | dom traversing js

분류에서Dev

When does Class#getClassLoader return null?

분류에서Dev

Sub Query - Return NULL, if it does not match the highest

분류에서Dev

iptables - why do I get "Table does not exist (do you need to insmod?)"

분류에서Dev

What to do from preventing SQLdatareader to return NULL?

분류에서Dev

Why does SomeStruct() is AnyObject return true?

분류에서Dev

Why does an async method not return the awaitable immediately?

분류에서Dev

Why does a void function return a value?

분류에서Dev

Why does egrep -o not return the entire match?

분류에서Dev

Why does my JavaScript instances return the same?

분류에서Dev

Why does this MySQL XOR query return 0?

분류에서Dev

Why does stripslashes not return the data without a slash?

분류에서Dev

Why does the error method return an error?

분류에서Dev

Why does 'return' and 'yield return' have the same behaviour in this example?

분류에서Dev

Why does this SQL order null values last?

분류에서Dev

Why does empty Bitbucket repo "contain work" that I "do not have locally"?

분류에서Dev

Linux sound: how does it work and why do I need to chain 3 architectures to use JACK?

분류에서Dev

Why does `systemctl enable rpcbind' change the volume of my sound? And how do I prevent this?

분류에서Dev

Why can I return references to objects?

분류에서Dev

I have a server application that gets data exactly half the time. Why/how does this happen and how do I fix it?

분류에서Dev

why do I receive the errror

분류에서Dev

Why do I get this NullExceptionError?

Related 관련 기사

  1. 1

    Why does SSIS lookup return null value?

  2. 2

    Why does this MySQL stored function return null?

  3. 3

    why does new Integer(i).hashCode() return i?

  4. 4

    Why does my code return wrong sum when i concatenate it?

  5. 5

    Why does new $class; return null when class_exists($class) returns true?

  6. 6

    Why does Vim delay on this remapped key? And how do I fix it?

  7. 7

    Why does ArrayList do this?

  8. 8

    why nextelementsibling return null | dom traversing js

  9. 9

    When does Class#getClassLoader return null?

  10. 10

    Sub Query - Return NULL, if it does not match the highest

  11. 11

    iptables - why do I get "Table does not exist (do you need to insmod?)"

  12. 12

    What to do from preventing SQLdatareader to return NULL?

  13. 13

    Why does SomeStruct() is AnyObject return true?

  14. 14

    Why does an async method not return the awaitable immediately?

  15. 15

    Why does a void function return a value?

  16. 16

    Why does egrep -o not return the entire match?

  17. 17

    Why does my JavaScript instances return the same?

  18. 18

    Why does this MySQL XOR query return 0?

  19. 19

    Why does stripslashes not return the data without a slash?

  20. 20

    Why does the error method return an error?

  21. 21

    Why does 'return' and 'yield return' have the same behaviour in this example?

  22. 22

    Why does this SQL order null values last?

  23. 23

    Why does empty Bitbucket repo "contain work" that I "do not have locally"?

  24. 24

    Linux sound: how does it work and why do I need to chain 3 architectures to use JACK?

  25. 25

    Why does `systemctl enable rpcbind' change the volume of my sound? And how do I prevent this?

  26. 26

    Why can I return references to objects?

  27. 27

    I have a server application that gets data exactly half the time. Why/how does this happen and how do I fix it?

  28. 28

    why do I receive the errror

  29. 29

    Why do I get this NullExceptionError?

뜨겁다태그

보관