"Current user did not login to the application!" 从服务调用函数时出错

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

在一项服务中,我编写了一个简单的函数来获取特定用户的租户 ID

    [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");
        }
    }

我是从帐户控制器的登录函数中调用此函数的。

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

我收到以下错误:

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

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

第一行有问题

 var qry = (from p in _memberRepository.GetAll()

.GetAll() 函数创建此错误,因为您不能在 linq 查询中使用内置函数。

改为使用

var Myvariable = _memberRepository.GetAll().ToList();
var qry = (from p in Myvariable where p.UserName == userNameOrEmail || p.EmailAddress == userNameOrEmail select p).FirstOrDefault();

问题是用户不属于租户。

使用以下行设置租户 ID 和代码有效。

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

向服务方法添加了 [AbpAllowAnonymous] 属性

问题是 AbpAuthorize 属性 [AbpAuthorize] 删除它。