Skip to content

Azure Function using C# instead of csx #1

Description

@anandsowm

For those who would like to do Home realm discovery using Azure Function, the run.csx provided in the project is based on c# script, if you wish to do it in C#, below is the equivalent code.

`` public static class HRD_Detect
{
[FunctionName("GetIdProvider")]
public static async Task Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("IdP selection received a request.");

        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
        dynamic reqdata = JsonConvert.DeserializeObject(requestBody);

        log.LogInformation($"request  : {reqdata}");
        if (reqdata.emailAddress == null)
        {
            log.LogInformation($"Empty request, email was null");
            return new OkResult();
        }

        var email = ((string)reqdata.emailAddress).ToLower();
        log.LogInformation($"email: {email}");
        char splitter = '@';
        string[] splitEmail = email.Split(splitter);
        var emailSuffix = splitEmail[1];
        if (email == "bar@foo.com")
        {
            log.LogInformation($"Identity Provider: aad");
            return new OkObjectResult(
                new ResponseContent
                {
                    version = "1.0.0",
                    status = (int)HttpStatusCode.OK,
                    userMessage = $"Your account is a generic Azure AD account.",
                    idp = "aad",
                    signInName = email
                });
        }

        //For B2C local accounts
        if (email == "foo@bar.com")
        {
            log.LogInformation($"Identity Provider: local");
            return new OkObjectResult(
                new ResponseContent
                {
                    version = "1.0.0",
                    status = (int)HttpStatusCode.OK,
                    userMessage = $"Your account seems to be a local account.",
                    idp = "local",
                    signInName = email
                });
        }

        //For Contoso AAD accounts
        if (emailSuffix == "contoso.com")
        {
            log.LogInformation($"Identity Provider: contoso");
            return new OkObjectResult(

                new ResponseContent
                {
                    version = "1.0.0",
                    status = (int)HttpStatusCode.OK,
                    userMessage = $"Your account belongs to the contoso Identity Provider",
                    idp = "contoso",
                    signInName = email
                });
        }
        else
        {
            log.LogInformation($"Identity Provider: none");
            return new OkObjectResult(
                new BlankContent
                {
                    status = (int)HttpStatusCode.OK,
                    signInName = email
                });
                
        }
        //return new OkResult();
    }

}

//Default responses where there is no match
public class BlankContent
{
    public int status { get; set; }
    public string signInName { get; set; }
}

//For responses where there is an IdP matching
public class ResponseContent
{
    public string version { get; set; }
    public int status { get; set; }
    public string userMessage { get; set; }
    public string idp { get; set; }
    public string signInName { get; set; }
}

}
``

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions