Coder Social home page Coder Social logo

Comments (6)

billba avatar billba commented on August 16, 2024 1

@seca23 that is extremely helpful, thank you!

from botframework-directlinejs.

seca23 avatar seca23 commented on August 16, 2024 1

I know this is an old thread, but here is a C# version for obtaining a token:

protected string file_get_contents(string url)
{
    var contents = "";
    if (url.ToLower().IndexOf("https:", StringComparison.Ordinal) > -1)
    {
        var wc = new System.Net.WebClient();
        var directlineSecret = ConfigurationManager.AppSettings["DirectlineSecret"].ToString();
        wc.Headers = new WebHeaderCollection {{"Authorization", string.Format("BotConnector {0}", directlineSecret) }};
        var response = wc.DownloadData(url);
        contents = System.Text.Encoding.ASCII.GetString(response);
    }
    else
    {
        var sr = new System.IO.StreamReader(url);
        contents = sr.ReadToEnd();
        sr.Close();
    }
    return contents;
}

from botframework-directlinejs.

billba avatar billba commented on August 16, 2024

from botframework-directlinejs.

hkusulja avatar hkusulja commented on August 16, 2024

@billba so Microsoft Example is to put secret inside JavaScript file. But you are telling that it should be used only by server backend which will obtain token and pass it to the JavaScript? What is best practice for public web site with Microsoft Bot ?

from botframework-directlinejs.

billba avatar billba commented on August 16, 2024

There are many scenarios where publishing the secret is no problem. All it really means is that anyone can put your bot on their web site. If what you want is for people to use your bot, that might be okay.

If you don't wish to publish your secret, then best practice is to have your website get it, and inject the token directly into the javascript of the web page it publishes. In this code, botchattest gets a token in the act of creating the web page containing WebChat.

Closing this issue.

from botframework-directlinejs.

seca23 avatar seca23 commented on August 16, 2024

@Hrcinjo

You can generate a token in WordPress with PHP like this:

<?php

/* This code is used to generate a token, that is only valid for a single conversation,
 * this prevents other developers from including your bot in their website. By 
 * restricting their access to your bot secret. 
 */

$botSecret = 'Your bot secret';

$response = wp_remote_get( 'https://webchat.botframework.com/api/tokens', array( 'headers' => 'Authorization: BotConnector ' . $botSecret ) );
if( is_array($response) ) {
  $header = $response['headers']; // array of http header lines
  $token = $response['body']; // use the content
}

?>

If you want to do this in a non WordPress site, you can do it like this:

$botSecret = 'Your bot secret';

$options = array('http' => array(
    'method' => 'GET',
    'header' => 'Authorization: BotConnector ' . $botSecret
));

$context = stream_context_create($options);

$token = file_get_contents('https://webchat.botframework.com/api/tokens', false, $context);

?>

from botframework-directlinejs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.