【问题标题】:Forbidden when I try to POST to Azure function当我尝试 POST 到 Azure 函数时被禁止
【发布时间】:2019-07-12 00:38:37
【问题描述】:

我已经部署了一个 Azure 函数。

函数应用程序受 Azure Active Directory 保护,因此我必须使用我的 AAD 凭据登录才能调用它。

当我对该函数进行 GET 调用时,一切都很好。

但是当我尝试进行 POST 调用时,我收到以下错误: 403 ForbiddenYou do not have permission to view this directory or page.

该函数是使用 Visual Studio 部署的,其入口点如下所示:

public static async Task<HttpResponseMessage> Run(
    [HttpTrigger(
        AuthorizationLevel.Anonymous, 
        "post", "get", 
        Route = null)] HttpRequestMessage req, 
    TraceWriter log)
{
    ...
}

【问题讨论】:

    标签: c# azure post azure-functions http-status-code-403


    【解决方案1】:

    它对我来说工作正常,以下是我所做的步骤:

    1. 使用以下代码从 Visual Studio 部署 azure function v1:
    [FunctionName("Function1")]
            public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
            {
                log.Info("C# HTTP trigger function processed a request.");
    
                // parse query parameter
                string name = req.GetQueryNameValuePairs()
                    .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
                    .Value;
    
                if (name == null)
                {
                    // Get request body
                    dynamic data = await req.Content.ReadAsAsync<object>();
                    name = data?.name;
                }
    
                return name == null
                    ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
                    : req.CreateResponse(HttpStatusCode.OK, "Hello " + name);
            }
    
    1. 使用 AAD 从 Portal 启用 EasyAUth。

    1. 从身份验证/授权刀片获取 AAD 应用程序的 ClientID 和 Client Secret。

    1. 使用下面的 Postman 调用获取 Token:

    1. 如上所示从邮递员那里获得令牌后,请使用下面所示的相同令牌调用 azure 函数:

    【讨论】:

      猜你喜欢
      • 2019-02-26
      • 2019-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-04
      • 2021-01-05
      相关资源
      最近更新 更多