Base solution for your next web application
Open Closed

TenantId getting cached. #1765


User avatar
0
sparkyjr created

Hi,

When testing for multiple Tenants the TenantId is getting cached in AbpSession. Just to recreate this scenario you could check AccountController, and try logging with different Tenants.


7 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    Are you logging in concurrently? If so, are you logging in the same browser tabs? If so, they share same cookies. Try using different browsers.

  • User Avatar
    0
    sparkyjr created

    Thank you for your reply!

    But when I login with two different Tenant users , two different tabs of the same browser why does UserManager.AbpSession.TenantId show me previous TenantId (from first tab of the browser) and not the latest one? And hence the Repositories and Services bring me data for the previous Tenant (from first tab of the browser) and not the current one.

    Also, I am using Background Worker to send out emails (every 24hours) with some data, specific to the Tenant. I couldn't add the worker in the pre-initialize method of the project module file. I need a TenantId, so, I was forced to add it in the Login method, and call an Application Service, where I need TenantId. Now, because of the issue explained in the paragraph above, I am getting wrong data.

    Am I doing anything wrong here? From where can I get the correct TenantId?

    Thanks in advance!

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    According to our tests, when we login from the second tab, it overrides the tenantId and when we refresh the first tab it gets second tab's data (I have tried with ABP 1.0 version referenced project).

    About the background job scenario, can you explain it in more details ? Why do you need TenantId for your background job ? Maybe instead of running a backgroundJob for each tenant, you can run a single backgroundJob and check for the tenants who fits your email sending criteria in every 24 hours.

    It is just an idea :), it might not fit into your scenario, we can discuss better after your explanation.

  • User Avatar
    0
    sparkyjr created

    Thanks for your reply!

    We are using Background Worker and not Background Job. We need to fire a method periodically to check if some data in some tables has been updated or not. If it has not been updated, we are sending out an email to the users informing them about the pending updates.

    Now, if I add/register the worker in the pre-initialize method in the project module file, then, it will be difficult for us to fetch the data, because, we don't get the TenantId there. So, we have added/registered the worker in the LogIn() method. The worker calls an Application Service and does everything for us.

    Hope this explanation helps!

    Thanks in advance!

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Can you share the code you register background worker ? Or even better if you can send your project with email.

    It might help to solve problem more quickly.

  • User Avatar
    0
    sparkyjr created

    Hi ,

    This is my code

    [HttpPost] [UnitOfWork] [DisableAuditing] public virtual async Task<JsonResult> Login(LoginViewModel loginModel, string returnUrl = "", string returnUrlHash = "") { CheckModelState();

            _unitOfWorkManager.Current.DisableFilter(AbpDataFilters.MayHaveTenant);
    
            var loginResult = await GetLoginResultAsync(loginModel.UsernameOrEmailAddress, loginModel.Password, loginModel.TenancyName);
    
            if (loginResult.User.ShouldChangePasswordOnNextLogin)
            {
                loginResult.User.SetNewPasswordResetCode();
    
                return Json(new MvcAjaxResponse
                {
                    TargetUrl = Url.Action(
                        "ResetPassword",
                        new ResetPasswordViewModel
                        {
                            UserId = SimpleStringCipher.Encrypt(loginResult.User.Id.ToString()),
                            ResetCode = loginResult.User.PasswordResetCode
                        })
                });
            }
    
            if (loginResult.User.TenantId.HasValue)
            {
                var trMap = await _tennantRhythmMapReository.FirstOrDefaultAsync(x => x.TenantId == (int)loginResult.User.TenantId);
    
                HttpCookie cookie = new HttpCookie("TrackingRhythmType");
                cookie.Value = (await _trackingRhythmReository.FirstOrDefaultAsync(x => x.Id == trMap.TrackingRhythmId)).TrackingRhythmType;
                Response.SetCookie(cookie);
    
                HttpCookie cookieTenantName = new HttpCookie("TenantName");
                cookieTenantName.Value = loginResult.User.Tenant.TenancyName;
                Response.SetCookie(cookieTenantName);
    
                _backgroundWorkerManager.Add(_actualUpdationDueWorker);
    
                // sets PersonalizedCaptionsCookie in PersonalizedCaptionsService.
                //await _personalizedCaptionService.SetPersonalizedCaptionsCookie(loginResult.User.TenantId);
            }
            else
            {
                var c = new HttpCookie("TenantName");
                c.Expires = DateTime.Now.AddDays(-1);
                Response.Cookies.Add(c);
            }
    
            await SignInAsync(loginResult.User, loginResult.Identity, loginModel.RememberMe);
    
            if (string.IsNullOrWhiteSpace(returnUrl))
            {
                returnUrl = Url.Action("Index", "Application");
            }
    
            if (!string.IsNullOrWhiteSpace(returnUrlHash))
            {
                returnUrl = returnUrl + returnUrlHash;
            }
    
            return Json(new MvcAjaxResponse { TargetUrl = returnUrl });
        }
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Can you move the line for adding background worker after this line and try again ?

    await SignInAsync(loginResult.User, loginResult.Identity, loginModel.RememberMe);