【问题标题】:Authentication for Proactive Message Endpoint主动消息端点的身份验证
【发布时间】:2020-08-10 02:07:28
【问题描述】:

我正在构建一个需要从 Azure 函数主动触发的 Bot Framework 机器人。我已经基于this example 将原型部署到Azure,目前允许我将POST 连接到/api/notify REST 端点,以便主动发送消息。

但是,我不确定如何将身份验证添加到 /api/notify 端点。发送到/api/messages 端点的消息使用应用程序代码中的 Bot Service API 进行身份验证,但在该示例中,没有对流向 /api/notify 端点的流量进行身份验证(我可以使用我的 CLI 中的curl 对其进行POST没有任何令牌或密码)。

我尝试在底层应用服务上启用App Service Authentication,但后来我的机器人不再在网络聊天中工作。

如何向此端点添加身份验证,以便只有我的 Azure Function 可以POST 向它添加身份验证?

【问题讨论】:

    标签: azure authentication azure-active-directory botframework azure-web-app-service


    【解决方案1】:

    实际上,几周前我刚刚为一个内部项目设置了这个。您可能需要将此策略调整为您的机器人和功能所使用的任何语言,但这是我所做的:

    Azure 函数

    module.exports = class BotService {
        constructor(context) {
            this.context = context;
            // Get appId and password from environment variables to build credentials
            this.credentials = new MicrosoftAppCredentials(process.env.MicrosoftAppId, process.env.MicrosoftAppPassword);
            this.client = axios.create({
                baseURL: process.env.BotBaseUrl
            });
        }
    
        async sendData(body) {
            // Get the auth token using the credentials
            const token = await this.credentials.getToken();
            const response = await this.client.post('/api/data', body, {
                // Add the token to the auth header
                headers: { Authorization: `Bearer ${ token }` }
            });
            if (response.status !== 200) {
                this.context.error(JSON.stringify(response, null, 2));
            } else {
                this.context.log(`Successfully sent data to the bot. Response Code: ${ response.status }`);
            }
        }
    }
    

    Bot 注意:该bot是在C#中,这是在/api/data的控制器中

    [HttpPost]
    public async Task<HttpStatusCode> PostAsync()
    {
        try
        {
            // Build the bot credentials
            var credentials = new SimpleCredentialProvider(Configuration["MicrosoftAppId"], Configuration["MicrosoftAppPassword"]);
            // Grab the auth header from the request
            Request.Headers.TryGetValue("Authorization", out StringValues authHeader);
            // Use Microsoft.Bot.Connector.Authentication.JwtTokenValidation to validate the auth header
            var result = await JwtTokenValidation.ValidateAuthHeader(authHeader, credentials, new SimpleChannelProvider(), Channels.Directline);
    
            if (result.IsAuthenticated)
            {
                // Do stuff
                // Do stuff
                return HttpStatusCode.OK;
            }
    
            return HttpStatusCode.Forbidden;
        }
        catch (Exception e)
        {
            Logger.LogError($"Something went wrong in /api/data controller: {e.Message}");
        }
        return HttpStatusCode.BadRequest;
    }
    

    看起来你的机器人是在 Python 中的。可以看similar auth validation in one of our Python tests

    【讨论】:

    • 很好的答案,Python 的相似之处很容易找到。
    • @evan.oman 哦,太好了!很高兴您找到它并感谢您的额外代表!
    猜你喜欢
    • 1970-01-01
    • 2014-12-25
    • 1970-01-01
    • 2012-08-15
    • 1970-01-01
    • 2021-03-26
    • 2018-10-15
    • 2019-08-20
    • 1970-01-01
    相关资源
    最近更新 更多