Open Closed

Usage of wrong Tenant Database #5957


1
kpmg created

Good morning,

we use multi tenancy with a host database and separate databases for several tenants. We encountered an issue when loading from the tenant database.

Some more details:

When a file is saved as binary (it could be other data types as well) with selected tenant X it cannot be loaded again in production. If we select the host, save and reload the file it works but it doesn't work on a tenant database.

The upload method is located in the C# Project

The download method is located in the same C# project in FileController also derived from

public async Task<FileResult> DownloadBinaryObject(Guid id, string displayName)
	    {
		    var binary = await _binaryObjectManager.GetOrNullAsync(id);
			var file = new FileContentResult(binary.Bytes, System.Net.Mime.MediaTypeNames.Application.Octet);
		    file.FileDownloadName = displayName;
		    return file;
	    }

The upload method:

        [HttpPost]
        [AbpMvcAuthorize]
        public async Task<JsonResult> UploadFile()
        {
            try
            {
                var file = Request.Form.Files.First();
                byte[] fileBytes;
                using (var stream = file.OpenReadStream())
                {
                    fileBytes = stream.GetAllBytes();
                }

                var fileObject = new BinaryObject(null, fileBytes);
                using (CurrentUnitOfWork.SetTenantId(AbpSession.TenantId))
                {
                    fileObject.TenantId = AbpSession.TenantId;
                    await BinaryObjectManager.SaveAsync(fileObject);
                }
                return Json(new AjaxResponse(new
                {
                    id = fileObject.Id,
                    name = file.FileName,
                    contentType = file.ContentType
                }));
            }
            catch (UserFriendlyException ex)
            {
                return Json(new AjaxResponse(new ErrorInfo(ex.Message)));
            }
        }

How can we resolve this issue?


3 Answer(s)
  • 0
    maliming created

    In the DownloadBinaryObject method, what is the TenantId of the AbpSession?

    and Why are you calling CurrentUnitOfWork.SetTenantId(AbpSession.TenantId) in theUploadFile

  • 0
    kpmg created

    If no tenant is selected, the tenant Id is null and the binary object is saved to the master database with tenant Id null. If a tenant is selected the correct tenant Id is set in the corresponding tenant database.

    Actually it is not my code but in this case it should not be a problem to set the tenant Id multiple times even if is not necessary.

  • 0
    ismcagdas created

    Hi @christianw

    There might be a bug here. Could you create an issue on https://github.com/aspnetzero/aspnet-zero-core/issues/new ?