Search code examples
asp.netasp.net-mvcboilerplateasp.net-boilerplate

"Current user did not login to the application!" error when function call from service


In a service I have wrote a simple function to get tenant id of particular user

    [AbpAuthorize]
    public int? FindTenancyNameByUserNameOrEmail(string userNameOrEmail)
    {
        var qry = (from p in _memberRepository.GetAll()
                   where p.UserName == userNameOrEmail || p.EmailAddress == userNameOrEmail
                   select p).FirstOrDefault();
        if (qry != null)
        {
            return qry.TenantId;
        }
        else
        {
            throw new Exception("User not found");
        }
    }

I am calling this function from login function of account controller.

    public async Task<JsonResult> Login(LoginViewModel loginModel, string returnUrl = "", string returnUrlHash = "")
    {
        var tenancyid = _memberAppService.FindTenancyNameByUserNameOrEmail(loginModel.UsernameOrEmailAddress.Trim());
        //bla bla code
    }

I get following error:

Exception thrown: 'Abp.Authorization.AbpAuthorizationException' in Abp.dll

Additional information: Current user did not login to the application!


Solution

  • The issue was the user was not belonging to the tenant.

    Used the following line to set the tenant id and code worked.

    CurrentUnitOfWork.SetFilterParameter(AbpDataFilters.MayHaveTenant, AbpDataFilters.Parameters.TenantId, intTenancyId);
    

    added the [AbpAllowAnonymous] attribute to the service method