【问题标题】:Communication service chat - Refresh token not work通信服务聊天 - 刷新令牌不起作用
【发布时间】:2020-11-05 12:11:24
【问题描述】:

我已经下载了 azure 通讯聊天服务,并且可以按照视频中所示的那样工作 https://www.youtube.com/watch?v=2pyKhoIZhJo.

但他们是问题..我如何克服令牌过期..它继续过期的treadID,我无法进一步聊天..

我已经提到了 RefreshToken,虽然它并不令人耳目一新。以下是我所做更改的代码,

 var userCredential = new CommunicationUserCredential
               (
                   initialToken: moderator.token,
                   refreshProactively: true,
                    tokenRefresher: cancellationToken => fetchNewTokenForCurrentUser(moderator.identity).GetAwaiter().GetResult(),
                    asyncTokenRefresher: cancellationToken => fetchNewTokenForCurrentUserasync(moderator.identity)
               );

fetchNewTokenForCurrentUser 方法如下

   private async Task<string> fetchNewTokenForCurrentUser( string identity)
        {
              CommunicationIdentityClient communicationIdentityClient = new CommunicationIdentityClient(resourceConnectionString);
                CommunicationUser user = new CommunicationUser(identity);
                Azure.Response<CommunicationUserToken> tokenResponse = await communicationIdentityClient.IssueTokenAsync
                                                                                (
                                                                                    user,
                                                                                    scopes: new[] { CommunicationTokenScope.Chat }

                                                                                );
                string token = tokenResponse.Value.Token;
            
                return token;
        }

但是这段代码不起作用.. 希望有人能帮助我..提前谢谢

【问题讨论】:

    标签: azure chat refresh-token azure-communication-services


    【解决方案1】:

    我们正在查看您报告的这个问题,需要更多详细信息。生成的令牌应该持续 24 小时。
    • 此问题是否发生在您的本地机器上,或者当演示在 azure 中部署为 Web 应用程序时?
    • 当您提到“treadID 持续过期”时,您看到的具体错误是什么?
    • 您有任何我们可以查看的错误跟踪吗?
    • 能否捕获 fiddler 会话(如果问题在本地可重现?)

    我们可以尝试的第一件事是对您的代码进行以下更改:

    const string initialToken = "your-initial-token";
    Uri apiUrl = new Uri("https://yourendpoint.dev.communication.azure.net");
    const string identity = "your user id"; //i.e. 8:acs:1b5cc06b-f352-4571-b1e6-d9b259b7c776_00000006-6224-f48d-b274-5aaaaaaaaaaa";
    
    var communicationUserCredential = new CommunicationUserCredential(
        initialToken: initialToken,
        refreshProactively: true,
        tokenRefresher: cancellationToken => fetchNewTokenForCurrentUser(identity).GetAwaiter().GetResult(),
        asyncTokenRefresher: cancellationToken => fetchNewTokenForCurrentUser(identity));
    
    var chatClient = new ChatClient(apiUrl, communicationUserCredential);
    
    
    //For the refresher logic. Note ValueTask vs Task
    private async ValueTask<string> fetchNewTokenForCurrentUser(string identity)
    {
        const string connectionString = "your connection string";
        CommunicationIdentityClient communicationIdentityClient = new CommunicationIdentityClient(connectionString);
        CommunicationUser user = new CommunicationUser(identity);
        Azure.Response<CommunicationUserToken> tokenResponse = await communicationIdentityClient.IssueTokenAsync
        (
            user,
            scopes: new[] { CommunicationTokenScope.Chat }
    
        );
        string token = tokenResponse.Value.Token;
        return token;
    }
    

    一旦您有机会提供上述详细信息,我们将与您联系。 干杯

    【讨论】:

      【解决方案2】:

      我猜你不仅需要更新版主令牌,还需要更新线程成员的令牌。

      例如,您有一个线程,其中包含 A、B、C 和主持人(这是在 C# 代码中创建的)。 24 小时后,所有用户的令牌都会过期。如果 A 仍想查看历史消息并发送消息,则需要更新 A 的令牌。同样的逻辑也适用于其他用户。

      希望它能回答你的问题。

      我看到一个更新令牌 pr 也在等待中:https://github.com/Azure-Samples/communication-services-web-chat-hero/pull/3

      【讨论】:

        猜你喜欢
        • 2021-11-26
        • 2017-06-24
        • 1970-01-01
        • 2017-07-18
        • 2021-08-24
        • 1970-01-01
        • 2020-03-31
        • 2018-01-06
        • 2017-04-19
        相关资源
        最近更新 更多