0

how do i get the user and password informed in basic authentication in azure functions ISOLATED?

exemple, using the app SOAPUI to make a call to the function:

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

how do i get in the function this USER and his password ?

[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;
        }

i also have a middleware how i get this info in him too?

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

i tried to search in the header, or the identities but i can't find this info/login and password

Bill Hileman
  • 2,798
  • 2
  • 17
  • 24
  • There is no way you are going to get `password` from any Azure API. You could get the authenticated user's name or email , if you have the JWT bearer token. The authentication system must have returned 'claims' which would be present in such a JWT token. – Anand Sowmithiran Dec 12 '22 at 13:09
  • Have you seen this https://stackoverflow.com/questions/44587136/how-to-get-current-user-identity-in-azure-function-with-azure-authentication ? – Anand Sowmithiran Dec 12 '22 at 13:10

1 Answers1

0

For Basic Authentication we need to change the open API security property values as below

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

Below is the screenshot of SoapUI

enter image description here

Authorization header need to be added in SoapUI as shown below enter image description here

Code In 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);
            }

Validation Code part

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;
Rajesh Mopati
  • 1,329
  • 1
  • 2
  • 7
  • with the header set manually it works, the problem is using only basic auth: https://i.imgur.com/sm4iOFu.png, https://i.imgur.com/FGWPFrQ.png, i need that it works only with the basic auth, in the raw view he did not send the auth: https://i.imgur.com/ydcMYyN.png, https://i.imgur.com/qCIKf60.png – Marcer ponts Dec 12 '22 at 15:56
  • I did like this https://i.imgur.com/7KeU1nA.png – Rajesh Mopati Dec 13 '22 at 02:51