Open Closed

Using Chat module - Unable to send chat message using impersonation and conversationAppService.SendMessageAsync #2396


User avatar
0
Tony_Albutt created

I want to have a user called System-Notifications. I want to use the Chat module to display notifications to users, and will contain a link to a task that they need to perform. What is the recommended method to use, so that I can send Chat notifications to users from within a Background Job?

Below, I have tested the conversationAppService.SendMessageAsync , and works from a web page calling an AppService method This does not work when I impersonate a user. How would I correct this?

I am unable to send a chat message using conversationAppService.SendMessageAsync when impersonating a user : using (_currentPrincipalAccessor.Change(newPrincipal)) I get an exception "Exception of type 'Volo.Abp.Authorization.AbpAuthorizationException' was thrown" when impersonating, although the impersonated CurrentUser.IsAuthenticated == true

If you're creating a bug/problem report, please include followings:

  • ABP Framework version: v4.4.4
  • UI type: **MVC **
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): no
  • Exception message and stack trace: Exception of type 'Volo.Abp.Authorization.AbpAuthorizationException' was thrown. **** below for the stack trace
  • Steps to reproduce the issue: This code rases the exception.
  • using (_currentPrincipalAccessor.Change(newPrincipal)) { var userName = CurrentUser.UserName; //See below **ii for contents await _conversationAppService.SendMessageAsync( // This is where the exception is triggered new SendMessageInput { TargetUserId = targetUserId, Message = args.Message, } ); }
  • * See below for the full code.

The purpose of the code is so that I can use the Chat module for In-App Notifications from a BackgroundJob

When I test from a web page calling an AppService, I can send the message as a logged in user using CurrentUser When I impersonate using currentPrincipalAccessor , I get an Exception of type 'Volo.Abp.Authorization.AbpAuthorizationException' was thrown.

* See below for my code.

    public async Task SendNotify(NotifyArgs args)
    {
        try
        {

            Guid.TryParse("54e53b63-14be-4e22-5a0b-39fe42671983", out var targetUserId);
            Guid.TryParse("5e328781-65e6-5d29-e83d-39fe195a5a22", out var senderUserId);

            //// Sends a SignalR message, does not save to DB, but works if the user is looking at the page
            //var chatMessage = new ChatMessageRdto()
            //{
            //    SenderUserId = senderUserId,
            //    SenderUsername = "user1@mail.com",
            //    SenderName = "User",
            //    SenderSurname = "One",
            //    Text = args.Message
            //};
            //await _realTimeChatMessageSender.SendAsync(targetUserId, chatMessage);

            //Sends a message and saves to DB
            var realUser = CurrentUser; //See below ** for content of CurrentUser
            var realUserName = CurrentUser.UserName; //returns "john"
            await _conversationAppService.SendMessageAsync(
               new SendMessageInput
               {
                   TargetUserId = targetUserId,
                   Message = args.Message,
               }
            );

            var newPrincipal = new ClaimsPrincipal(
                                    new ClaimsIdentity(
                                        new Claim[]
                                        {
                                                new Claim(AbpClaimTypes.TenantId, CurrentUser.TenantId.ToString()),
                                                new Claim(AbpClaimTypes.UserId, CurrentUser.Id.ToString()),
                                                new Claim(AbpClaimTypes.ImpersonatorTenantId, CurrentUser.TenantId.ToString()),
                                                new Claim(AbpClaimTypes.ImpersonatorUserId, "3e1c2476-ad49-85c7-9101-39fe18cffd26"),
                                                new Claim(AbpClaimTypes.Email, "user1@mail.com"),
                                                new Claim(AbpClaimTypes.UserName, "user1@mail.com"),
                                                new Claim(AbpClaimTypes.Name, "User"),
                                                new Claim(AbpClaimTypes.SurName, "One"),
                                                new Claim(AbpClaimTypes.Role, "Traveller")
                                        }
                                    )
                                );

            using (_currentPrincipalAccessor.Change(newPrincipal))
            {
                var userName = CurrentUser.UserName; //See below *** for content of CurrentUser
                await _conversationAppService.SendMessageAsync( // This is where the exception is triggered
                   new SendMessageInput
                   {
                       TargetUserId = targetUserId,
                       Message = args.Message,
                   }
               );
            }

        }
        catch (Exception ex)
        {
            //throw;
        }

    }

** realUser = CurrentUser

*** CurrentUser = newPrincipal

**** Exception message and stack trace


1 Answer(s)
  • User Avatar
    0
    yekalkan created
    Support Team Fullstack Developer

    Hi @Tony_Albutt,

    This is not an expected usage of the chat module. I can't help you with that.

    We've a Real-time Notification System on our roadmap, though release date is not set.

Made with ❤️ on ABP v9.1.0-preview. Updated on November 01, 2024, 05:35