简体   繁体   English

从对话框到机器人类的身份验证令牌

[英]Auth token from dialog to bot class

I am running an OAuth Dialog that allows user to sign in. I am looking to get this Auth token from DialogsClass.cs to my Bot.Cs class file and use it to make Graph calls. 我正在运行一个允许用户登录的OAuth对话框。我希望将Auth令牌从DialogsClass.cs获取到我的Bot.Cs类文件,并使用它进行Graph调用。

I have tried to save token as string in local file within my dialog class and then read it back in main bot class but this solution does not seems as a right way of doing it. 我试图将令牌另存为字符串,并将其保存在我的对话框类中的本地文件中,然后在bot主类中将其读回,但是此解决方案似乎不是正确的方法。

AuthDialog.cs in Waterfall step: Waterfall步骤中的AuthDialog.cs:

var tokenResponse = (TokenResponse)stepContext.Result;

Expected result. 预期结果。 Transfer this token from Dialog class to MainBot.cs class and use as string to make Graph calls. 将此标记从Dialog类转移到MainBot.cs类,并用作字符串进行Graph调用。

Are you using one waterfall step to get token with OAuthPrompt and then another step to call a different class (in which you do graph api calls)? 您是否正在使用一个瀑布步骤来使用OAuthPrompt获取令牌,然后又使用另一步骤来调用其他类(您在其中进行图形api调用)? Why can't you just pass the token to the down stream class? 您为什么不能仅将令牌传递给下游类?

If there are other steps in the middle, there are multiple ways to resolve it: 如果中间还有其他步骤,则有多种解决方法:

Microsoft suggests not to store token in the system but make a call to oAuth prompt return await stepContext.BeginDialogAsync(nameof(OAuthPrompt), null, cancellationToken); 微软建议不要将令牌存储在系统中,而是调用oAuth提示符return await stepContext.BeginDialogAsync(nameof(OAuthPrompt), null, cancellationToken); and get latest token whenever you have to call Graph API. 并在需要调用Graph API时获取最新令牌。 Once you receive the token in var tokenResponse = (TokenResponse)stepContext.Result; var tokenResponse = (TokenResponse)stepContext.Result;收到令牌后, var tokenResponse = (TokenResponse)stepContext.Result; you can make a call to GraphClient class which will create the Graph API client using the token in Authorization attribute. 您可以调用GraphClient类,该类将使用Authorization属性中的令牌创建Graph API客户端。

var client = new GraphClientHelper(tokenResponse.Token);

Graph Client implementation: Graph Client的实现:

  public GraphClientHelper(string token)
    {
        if (string.IsNullOrWhiteSpace(token))
        {
            throw new ArgumentNullException(nameof(token));
        }
        _token = token;
    }
   private GraphServiceClient GetAuthenticatedClient()
    {
        var graphClient = new GraphServiceClient(
            new DelegateAuthenticationProvider(
                requestMessage =>
                {
                    // Append the access token to the request.
                    requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", _token);

                    // Get event times in the current time zone.
                    requestMessage.Headers.Add("Prefer", "outlook.timezone=\"" + TimeZoneInfo.Local.Id + "\"");

                    return Task.CompletedTask;
                }));
        return graphClient;
    }

Once graph client is created you can make a call to the intended graph api: 创建图客户端后,您可以调用所需的图API:

 await client.CreateMeeting(meetingDetails).ConfigureAwait(false);

Please refer this sample code: Graph Sample 请参考以下示例代码: 图形示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM