调试时从函数应用程序向eventgrid发送消息时出现未经授权的错误,但在Azure中运行时正常

j1dl9f46  于 2022-12-04  发布在  其他
关注(0)|答案(1)|浏览(105)

我有一个函数应用程序,其中的函数可将消息发送到事件网格。该函数应用程序已注册并获得RBAC授权。此同一函数应用程序中的函数已订阅此事件网格主题。当我将该应用程序发布到Azure并远程运行它时,它工作正常。但是当我在VS 2022中本地运行它调试时,我得到了未经授权的访问来发送消息。VS 2022中的调试对于我们所做的其他Azure相关的事情来说工作得很好。

当我在VS 2022的调试模式下执行函数应用程序以将数据发送到事件网格时,我收到未经授权的错误:

//Name of the endpoint of Event grid topic
    string topicEndpoint = transformAlgoSendRMessage_TopicEP;
    //Creating client to publish events to eventgrid topic
    EventGridPublisherClient client = new EventGridPublisherClient(new Uri(topicEndpoint), new DefaultAzureCredential());
    //Creating a sample event with Subject, Eventtype, dataVersion and data
    EventGridEvent egEvent = new EventGridEvent("TransformTelemetry", "TransformAlgorithm.broadcastTransform", "1.0", machinePartTransformTelemetry);
    // Send the event
    
    try
    {
        await client.SendEventAsync(egEvent);
        if (b_debug_contractor)
            log.LogInformation("SendRTransformMessage sent transformdata - PosX:" + machinePartTransformTelemetry[1]);
    }
    catch (Exception e)
    {
        log.LogError("Failed to send SendRTransformMessage. " + e.Message);
    }

未授权错误:

Status: 401 (The principal associated with access token presented with the incoming request does not have permission to send data to /subscriptions/mysubscriptionid/resourceGroups/myRG/providers/Microsoft.EventGrid/topics/myfunctionapp. Report '3840bb30-0b52-4869-a3ce-40ef9119ed42:2:11/28/2022 7:44:09 PM (UTC)' to our forums for assistance or raise a support ticket.)
[2022-11-28T19:44:07.324Z] ErrorCode: Unauthorized
[2022-11-28T19:44:07.324Z]
[2022-11-28T19:44:07.324Z] Content:
[2022-11-28T19:44:07.325Z] {
[2022-11-28T19:44:07.325Z]     "error": {
[2022-11-28T19:44:07.326Z]         "code": "Unauthorized",
[2022-11-28T19:44:07.326Z]         "message": "The principal associated with access token presented with the incoming request does not have permission to send data to /subscriptions/mysubscriptionid/resourceGroups/myRG/providers/Microsoft.EventGrid/topics/myfunctionapp. Report '3840bb30-0b52-4869-a3ce-40ef9119ed42:2:11/28/2022 7:44:09 PM (UTC)' to our forums for assistance or raise a support ticket.",
[2022-11-28T19:44:07.327Z]         "details": [{
[2022-11-28T19:44:07.327Z]             "code": "Unauthorized",
[2022-11-28T19:44:07.328Z]             "message": "The principal associated with access token presented with the incoming request does not have permission to send data to /subscriptions/mysubscriptionid/resourceGroups/myRG/providers/Microsoft.EventGrid/topics/myfunctionapp. Report '3840bb30-0b52-4869-a3ce-40ef9119ed42:2:11/28/2022 7:44:09 PM (UTC)' to our forums for assistance or raise a support ticket."
[2022-11-28T19:44:07.328Z]     }]
[2022-11-28T19:44:07.329Z]   }
[2022-11-28T19:44:07.329Z] }
[2022-11-28T19:44:07.330Z]
[2022-11-28T19:44:07.330Z] Headers:
[2022-11-28T19:44:07.330Z] Server: Microsoft-HTTPAPI/2.0
[2022-11-28T19:44:07.331Z] Strict-Transport-Security: REDACTED
[2022-11-28T19:44:07.331Z] x-ms-request-id: 3840bb30-0b52-4869-a3ce-40ef9119ed42
[2022-11-28T19:44:07.332Z] Date: Mon, 28 Nov 2022 19:44:08 GMT
[2022-11-28T19:44:07.332Z] Content-Length: 941
[2022-11-28T19:44:07.333Z] Content-Type: application/json; charset=utf-8

mysubscriptionid如功能应用界面所示:

我正在使用Microsoft Visual Studio社区2022(64位)-当前版本17.4.1

rqqzpn5f

rqqzpn5f1#

该异常是由try语句中的日志语句引起的,与发送到eventgrid无关,我不能告诉这是如何停止被授权的,现在它工作了。

if (b_debug_contractor)
        log.LogInformation("SendRTransformMessage sent transformdata - PosX:" + machinePartTransformTelemetry[1]);

相关问题