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.
I see the problem:
The active user during testing remains logged in from the previous debugging session.
인증 쿠키에는 이전 세션의 사용자 ID에 대한 Guid가 포함됩니다. 그리고 매번 사용자를 다시 생성하면 userId에 대한 guid가 데이터베이스에서 변경되고 쿠키에 저장된 Id와 일치하지 않습니다. 따라서 모든 디버그 세션에서 사용자를 다시 생성하거나 모든 디버그에서 쿠키를 종료하지 마십시오.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다