簡體   English   中英

如何在隔離的 azure 函數上使用基本身份驗證?

[英]how to use basic authentication on azure functions ISOLATED?

我如何在 azure 功能隔離的基本身份驗證中獲取用戶和密碼?

例如,使用應用程序 SOAPUI 調用 function:

[https://i.imgur.com/3u7eymT.png]

我如何進入 function 這個用戶和他的密碼?

[Function("Function1")]
        public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req)
        {
            _logger.LogInformation("C# HTTP trigger function processed a request.");

            var response = req.CreateResponse(HttpStatusCode.OK);
            response.Headers.Add("Content-Type", "text/plain; charset=utf-8");

            response.WriteString("Welcome to Azure Functions!");

            return response;
        }

我也有一個中間件,我如何從他那里得到這些信息?

public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next)
        {

我試圖在 header 或身份中搜索,但找不到此信息/登錄名和密碼

對於基本身份驗證,我們需要更改打開的 API 安全屬性值,如下所示

[OpenApiSecurity("basic_auth", SecuritySchemeType.Http, Scheme = OpenApiSecuritySchemeType.Basic)]

下面是SoapUI的截圖

在此處輸入圖像描述

SoapUI中需要添加授權header如下圖在此處輸入圖像描述

代碼在 Function

 var headers = req.Headers["Authorization"];
            if (ValidateToken(headers))
            {
                string name = req.Query["name"];
                string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
                dynamic data = JsonConvert.DeserializeObject(requestBody);
                name = name ?? data?.name;
                string responseMessage = string.IsNullOrEmpty(name) ? "Pass a name in the query string" : $"{name}";
                    return new OkObjectResult(responseMessage);
            }

驗證碼部分

string encode_Credentials = header.Substring("Basic ".Length).Trim();
                Encoding encode = Encoding.GetEncoding("iso-8859-1");
                string credentials = encode.GetString(Convert.FromBase64String(encode_Credentials));
                int seperatorIndex = credentials.IndexOf(':');
                var username = credentials.Substring(0, seperatorIndex);
                var password = credentials.Substring(seperatorIndex + 1);
                if (username is "Rajesh" && password is "1142") return true;
                else return false;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM