azure - 主动消息端点的身份验证

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

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

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

我尝试在底层应用服务上启用应用服务身份验证,但随后我的机器人不再在网络聊天中工作。

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

最佳答案

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

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

机器人 注意:机器人采用 C# 语言,位于 /api/data 的 Controller 中

[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

关于azure - 主动消息端点的身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63333219/

相关文章:

azure - LUIS 列出实体

azure - 有没有办法为同一Azure数据工厂中的管道和数据集设置不同的权限?

ios - 为同一个新对象多次调用 Firebase FEventTypeChildAdded 回调

c# - 使用 Windows 身份验证,没有弹出窗口

elasticsearch - 在 NEST/Elasticsearch.net 中使用 azure 事件目录

c# - Microsoft Graph API 和 Azure AD SSO 的问题

azure - 通配符路径的 ADF 复制事件问题

azure - 如何在Windows Azure中创建队列?

javascript - 使用 PHP 登录 Google+

azure - 将 MVC 授权属性与使用 Azure Active Directory + OWIN 的角色结合使用