0
votes

based on this Bot DirectLine Authentication,

If you plan to distribute the token to clients and want them to initiate the conversation, use the Generate Token operation.

Does this mean we can generate token from backend using Secret and distribute the token to the client for starting a conversation?

To test it, I wrote these:

Backend: @Azure Function

  [FunctionName("XXXXX")]
    public static async Task<object> RunAsync([HttpTrigger(Route = "XXXXX")] HttpRequestMessage req, TraceWriter log)
    {
        log.Info($"Webhook was triggered!");

        var tokenResponse = await new DirectLineClient(directLineSecret).Tokens.GenerateTokenForNewConversationAsync();
        return req.CreateResponse(HttpStatusCode.OK, tokenResponse.Token);
    }

and

Client @UWP
// token from Backend
directLineClient = new DirectLineClient(token);
var conversation = directLineClient.Conversations.StartConversation();

weird thing is the variable conversation is null.

When I put the Generate Token code of Backend to Client, it works that the variable conversation is a valid object.

my question is: can we put the Generate Token in backend and distribute token to clients?

2
Any exception when you use it from the backend?Nicolas R
Much appreciated. No exception in the backend. The mechanism is correct. The buggy part I found is the backend code that I didn't add Media Type in the response which makes the token being misunderstood that the token is a json and being added unnecessary wrapped quotes in the token.Paul

2 Answers

1
votes

my question is: can we put the Generate Token in backend and distribute token to clients?

Of course, if you do not want to expose/share Direct line secret publicly, you can generate Token in backend service and distribute the token to clients and let them to initiate the conversation.

Note: please make sure the Direct Line token generated by your http triggered function is not expired when you use it in your DirectLine Client.

I do a test with DirectLineClient of Direct Line Bot Sample, and modify the code to accept the Direct Line token. If I provide a Direct Line token that is expired, starting conversation will fail.

enter image description here

0
votes

The problem is that the backend should return token as text media type. much appreciated for your feedback and reminding.

 return req.CreateResponse(HttpStatusCode.OK, tokenResponse.Token, "text/plain");