Home > front end >  how to use basic authentication on azure functions ISOLATED?
how to use basic authentication on azure functions ISOLATED?

Time:12-14

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

CodePudding user response:

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;

CodePudding user response:

'You must have 50 reputation to comment' and i cant even edit de OG post 'There are too many pending edits on Stack Overflow. Please try again later.'

so i will post as 'reply'

@RajeshM you answer seams to be on a inline function that code do not work in ISOLATED function

@Anand Sowmithiran i do not understand what you are saying, but i mean get the token/user and password from header generally, and i always did that normally be it encoded in base64 or with some custom cryptography

and the link that you posted is not for ISOLATED, that don't work in isolated function is a complete different code

what i wish is simple, that. this:

https://i.imgur.com/ydcMYyN.png https://i.imgur.com/qCIKf60.png

that works fine in a webapp .net core old project, works also in azure function ISOLATED, but azure function isolated don't seams to support that kind of auth

edit---

ok i make it work, but is not ideal, how i make it work? not a single new code, just check per-empitively on soap-ui: https://i.imgur.com/LvWbaO6.png https://i.imgur.com/7v0NqkL.png

the header as created automatically and i can get the user and password in base64 in the traditional format:

VVNFUjI6cGFzc3dvcmQ= https://www.base64decode.org/ USER2:password

but the problem remains....why it works in the old app with the option 'use global preference' and not with azure function isolated?

  • Related