Azure Function Login Sample

This example shows a simple example of an authentication form that will gather respondent credentials, validate the credentials remotely using an Azure's function and return the full information of the user to the survey.

First start by setting up the user interface of your security function to ask the username and password from respondent.

Then log into your Azure online portal to create a new Azure functions that will get the data posted by the user interface we have just created as a json object eg: {username:'value', password:'value'} and return a proper user object if the credential are valid.

ngSurvey expect to receive from the end point a simple json object eg: {name:'value', firstName:'value'}, all these properties of the object will be saved along respondent answers and can be used and piped anywhere in questions / answers using __lowercasepropertyname__ . In our example we can use __name__ to pipe the name returned by our azure function in any part of our survey.

Any kind of errors that must be returned from the http end point must be returned as an Http Bad Request with the message that will be shown to the respondent

Here our basic Azure function code that will check for the fake user credentials

#r "Newtonsoft.Json"

  using System.Net;
  using Microsoft.AspNetCore.Mvc;
  using Microsoft.Extensions.Primitives;
  using Newtonsoft.Json;
  
  class User {
      public string Id; 
      public string Email;
      public string Name;
  }
  public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
  {
      string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
      dynamic data = JsonConvert.DeserializeObject(requestBody);
      // check user 
      if (data?.username == "JohnDoe" && data?.password =="MayTheForceBeWithYou") {
        // If user valid return an object  
        var user = new User();
          user.Id = "1234567";
          user.Email = "John@Doe.com";
          user.Name = "John doe";
          return (ActionResult)new OkObjectResult(user);
      } else {
          return new BadRequestObjectResult("Invalid user!");
      }
  }

That's it! You just extended ngSurvey with some new custom login business logic using a remote Azure function.

Last updated

(c) 2024 Data Illusion Zumbrunn. All rights reserved. Reproduction strictly forbidden.