当我尝试发布到Azure功能时被禁止

时间:2019-02-18 15:27:00

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

我已经部署了Azure功能。

该功能应用程序受Azure Active Directory保护,因此要调用该应用程序,我必须使用我的AAD凭据登录。

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

但是当我尝试进行POST呼叫时,出现以下错误: 403 Forbidden You 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)
{
    ...
}

1 个答案:

答案 0 :(得分:1)

它对我来说很好,下面是我所做的步骤:

  1. 使用以下代码从Visual Studio部署了Azure功能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。

EasyAuthImage

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

ClientID/ClientSceret

  1. 使用以下邮递员电话获取令牌:

Postman

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

FunctionCall