abp icon indicating copy to clipboard operation
abp copied to clipboard

IdentityUser Claims Error

Open wayneboyles opened this issue 7 years ago • 11 comments

Hi, I am receiving the following error when logging in to my application:

InvalidOperationException: Navigation property 'Claims' on entity of type 'IdentityUser' cannot be loaded because the entity is not being tracked. Navigation properties can only be loaded for tracked entities.

Microsoft.EntityFrameworkCore.Internal.EntityFinder<TEntity>.LoadAsync(INavigation navigation, InternalEntityEntry entry, CancellationToken cancellationToken)
Volo.Abp.Domain.Repositories.EntityFrameworkCore.EfCoreRepository<TDbContext, TEntity>.EnsureCollectionLoadedAsync<TProperty>(TEntity entity, Expression<Func<TEntity, IEnumerable<TProperty>>> propertyExpression, CancellationToken cancellationToken) in EfCoreRepository.cs
Volo.Abp.Domain.Repositories.RepositoryExtensions.EnsureCollectionLoadedAsync<TEntity, TKey, TProperty>(IBasicRepository<TEntity, TKey> repository, TEntity entity, Expression<Func<TEntity, IEnumerable<TProperty>>> propertyExpression, CancellationToken cancellationToken) in RepositoryExtensions.cs
Volo.Abp.Identity.IdentityUserStore.GetClaimsAsync(IdentityUser user, CancellationToken cancellationToken) in IdentityUserStore.cs
Microsoft.AspNetCore.Identity.UserManager<TUser>.GetClaimsAsync(TUser user)
Microsoft.AspNetCore.Identity.UserClaimsPrincipalFactory<TUser>.GenerateClaimsAsync(TUser user)
Microsoft.AspNetCore.Identity.UserClaimsPrincipalFactory<TUser, TRole>.GenerateClaimsAsync(TUser user)
Microsoft.AspNetCore.Identity.UserClaimsPrincipalFactory<TUser>.CreateAsync(TUser user)
Volo.Abp.Identity.AbpUserClaimsPrincipalFactory.CreateAsync(IdentityUser user) in AbpUserClaimsPrincipalFactory.cs
Volo.Abp.Uow.UnitOfWorkInterceptor.InterceptAsync(IAbpMethodInvocation invocation) in UnitOfWorkInterceptor.cs
Volo.Abp.Castle.DynamicProxy.CastleAbpInterceptorAdapter<TInterceptor>.ExecuteWithReturnValueAsync<T>(IInvocation invocation, IInvocationProceedInfo proceedInfo) in CastleAbpInterceptorAdapter.cs
Microsoft.AspNetCore.Identity.SignInManager<TUser>.CreateUserPrincipalAsync(TUser user)
Microsoft.AspNetCore.Identity.SignInManager<TUser>.SignInAsync(TUser user, AuthenticationProperties authenticationProperties, string authenticationMethod)
Microsoft.AspNetCore.Identity.SignInManager<TUser>.SignInOrTwoFactorAsync(TUser user, bool isPersistent, string loginProvider, bool bypassTwoFactor)
Microsoft.AspNetCore.Identity.SignInManager<TUser>.PasswordSignInAsync(TUser user, string password, bool isPersistent, bool lockoutOnFailure)
Microsoft.AspNetCore.Identity.SignInManager<TUser>.PasswordSignInAsync(string userName, string password, bool isPersistent, bool lockoutOnFailure)
GranadeManagement.Web.Pages.Account.LoginModel.OnPostLoginAsync(LoginInputModel loginInput) in Login.cshtml.cs

This only occurs on a successful login - if I enter invalid credentials then it does the user lookup and returns a failed result as expected with no error.

wayneboyles avatar Jun 30 '19 17:06 wayneboyles

Please share the code for your OnPostLoginAsync method.

maliming avatar Jul 01 '19 01:07 maliming

@maliming Thanks for the reply. What I've done is taken the Login code from the Account Web module because I wanted to have a totally different login page.

Here's the contents of the method that get's called on POST. As I mentioned, if you get the credentials wrong it correctly detects that so the SignInManager appears to be working in terms of checking credentials... it's just when you get it right it fails.

[UnitOfWork]
public async Task<IActionResult> OnPostLoginAsync(LoginInputModel loginInput)
{
	EnableLocalLogin = true; //TODO: We can get default from a setting?

	ValidateModel();

	await ReplaceEmailToUsernameOfInputIfNeeds();

	var result = await SignInManager.PasswordSignInAsync(
		LoginInput.UserNameOrEmailAddress,
		LoginInput.Password,
		LoginInput.RememberMe,
		true
	);

	if (result.RequiresTwoFactor)
	{
		return RedirectToPage("./SendSecurityCode", new
		{
			returnUrl = ReturnUrl,
			returnUrlHash = ReturnUrlHash,
			rememberMe = LoginInput.RememberMe
		});
	}

	if (result.IsLockedOut)
	{
		Alerts.Warning(L["UserLockedOutMessage"]);
		return Page();
	}

	if (result.IsNotAllowed)
	{
		Alerts.Warning(L["LoginIsNotAllowed"]);
		return Page();
	}

	if (!result.Succeeded)
	{
		Alerts.Danger(L["InvalidUserNameOrPassword"]);
		return Page();
	}

	//TODO: Find a way of getting user's id from the logged in user and do not query it again like that!
	var user = await UserManager.FindByNameAsync(LoginInput.UserNameOrEmailAddress) ??
			   await UserManager.FindByEmailAsync(LoginInput.UserNameOrEmailAddress);

	Debug.Assert(user != null, nameof(user) + " != null");

	return RedirectSafely(ReturnUrl, ReturnUrlHash);
}

Although my project contains some names etc. I wouldn't want to make public I don't mind sharing my entire solution with you privately if required if that will help at all.

wayneboyles avatar Jul 01 '19 15:07 wayneboyles

You have not copied the code completely. 😄

image

maliming avatar Jul 02 '19 01:07 maliming

Good lord I'm blind. Thank you!

wayneboyles avatar Jul 03 '19 01:07 wayneboyles

Why do I need to enter the virutal keyword @maliming @wayneboyles

Loongle avatar Dec 28 '19 07:12 Loongle

Class interception requires the methods being intercepted to be virtual since it uses subclassing as the proxy technique.

https://autofaccn.readthedocs.io/en/latest/advanced/interceptors.html

maliming avatar Dec 29 '19 07:12 maliming

I'm trying to use SignInManager like below in Blazor page and get same Navigation property Claims error. How to fix this? Thanks @maliming @hikalkan

[Inject] public SignInManager<Volo.Abp.Identity.IdentityUser> SignInManager { get; set; }
    
public string ReturnUrl { get; set; }

public LoginInputModel LoginInput { get; set; }

protected virtual async Task Login()
{  
     var result = await SignInManager.PasswordSignInAsync(
                    LoginInput.UserNameOrEmailAddress,
                    LoginInput.Password,
                    LoginInput.RememberMe,
                    true
                );
}

Alexbits avatar Jan 05 '20 19:01 Alexbits

hi @Alexbits I'm not very familiar with Blazor page, it may not be intercepted by mvc's action filter.

You can use the [UnitOfWork] attribute on methods.

maliming avatar Jan 06 '20 01:01 maliming

Hi @maliming. I tried this. Same exception with UnitOfWork attribute.

   at Microsoft.EntityFrameworkCore.Internal.EntityFinder`1.LoadAsync(INavigation navigation, InternalEntityEntry entry, CancellationToken cancellationToken)
   at Volo.Abp.Domain.Repositories.EntityFrameworkCore.EfCoreRepository`2.EnsureCollectionLoadedAsync[TProperty](TEntity entity, Expression`1 propertyExpression, CancellationToken cancellationToken)
   at Volo.Abp.Domain.Repositories.RepositoryExtensions.EnsureCollectionLoadedAsync[TEntity,TKey,TProperty](IBasicRepository`2 repository, TEntity entity, Expression`1 propertyExpression, CancellationToken cancellationToken)
   at Volo.Abp.Identity.IdentityUserStore.GetClaimsAsync(IdentityUser user, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Identity.UserManager`1.GetClaimsAsync(TUser user)
   at Microsoft.AspNetCore.Identity.UserClaimsPrincipalFactory`1.GenerateClaimsAsync(TUser user)
   at Microsoft.AspNetCore.Identity.UserClaimsPrincipalFactory`2.GenerateClaimsAsync(TUser user)
   at Volo.Abp.Uow.UnitOfWorkInterceptor.InterceptAsync(IAbpMethodInvocation invocation)
   at Volo.Abp.Castle.DynamicProxy.CastleAbpInterceptorAdapter`1.ExecuteWithReturnValueAsync[T](IInvocation invocation, IInvocationProceedInfo proceedInfo)
   at Microsoft.AspNetCore.Identity.UserClaimsPrincipalFactory`1.CreateAsync(TUser user)
   at Volo.Abp.Identity.AbpUserClaimsPrincipalFactory.CreateAsync(IdentityUser user)
   at Volo.Abp.Uow.UnitOfWorkInterceptor.InterceptAsync(IAbpMethodInvocation invocation)
   at Volo.Abp.Castle.DynamicProxy.CastleAbpInterceptorAdapter`1.ExecuteWithReturnValueAsync[T](IInvocation invocation, IInvocationProceedInfo proceedInfo)
   at IdentityServer4.AspNetIdentity.UserClaimsFactory`1.CreateAsync(TUser user)
   at Microsoft.AspNetCore.Identity.SignInManager`1.CreateUserPrincipalAsync(TUser user)
   at Microsoft.AspNetCore.Identity.SignInManager`1.SignInWithClaimsAsync(TUser user, AuthenticationProperties authenticationProperties, IEnumerable`1 additionalClaims)
   at Microsoft.AspNetCore.Identity.SignInManager`1.SignInOrTwoFactorAsync(TUser user, Boolean isPersistent, String loginProvider, Boolean bypassTwoFactor)
   at Microsoft.AspNetCore.Identity.SignInManager`1.PasswordSignInAsync(TUser user, String password, Boolean isPersistent, Boolean lockoutOnFailure)
   at Microsoft.AspNetCore.Identity.SignInManager`1.PasswordSignInAsync(String userName, String password, Boolean isPersistent, Boolean lockoutOnFailure)

Alexbits avatar Jan 06 '20 07:01 Alexbits

hi @Alexbits Can you share a project that reproduces the problem?

maliming avatar Jan 06 '20 07:01 maliming

Sorry I can't at the moment

Alexbits avatar Jan 06 '20 12:01 Alexbits