0

我已经下载了 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;
        }

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

4

2 回答 2

0

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

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

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;
}

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

于 2020-11-13T17:23:48.330 回答
0

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

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

希望它能回答你的问题。

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

于 2020-11-16T20:44:24.390 回答