Created
March 19, 2024 07:30
-
-
Save astaykov/6f8f536400d582ea1da9084093735b5a to your computer and use it in GitHub Desktop.
Azure Function code for Entra ID custom authentication extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "Newtonsoft.Json" | |
using System.Net; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Extensions.Primitives; | |
using Newtonsoft.Json; | |
public static async Task<IActionResult> Run(HttpRequest req, ILogger log) | |
{ | |
log.LogInformation("C# HTTP trigger function processed a request."); | |
string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); | |
log.LogInformation("Request Headers are: "); | |
string hrds = ""; | |
foreach(var header in req.Headers) | |
{ | |
hrds += $" {header.Key} :: "; | |
foreach(var value in header.Value) | |
{ | |
hrds += $"{value}, "; | |
} | |
hrds += "\n<br>"; | |
} | |
log.LogInformation(hrds); | |
log.LogInformation("Request Body is: "); | |
log.LogInformation(requestBody); | |
dynamic data = JsonConvert.DeserializeObject(requestBody); | |
// Read the correlation ID from the Azure AD request | |
string correlationId = data?.data.authenticationContext.correlationId; | |
// Claims to return to Azure AD | |
ResponseContent r = new ResponseContent(); | |
r.data.actions[0].claims.CorrelationId = correlationId; | |
r.data.actions[0].claims.ApiVersion = "1.0.0"; | |
r.data.actions[0].claims.DateOfBirth = "01/01/2000"; | |
r.data.actions[0].claims.CustomRoles.Add("Writer"); | |
r.data.actions[0].claims.CustomRoles.Add("Editor"); | |
return new OkObjectResult(r); | |
} | |
public class ResponseContent{ | |
[JsonProperty("data")] | |
public Data data { get; set; } | |
public ResponseContent() | |
{ | |
data = new Data(); | |
} | |
} | |
public class Data{ | |
[JsonProperty("@odata.type")] | |
public string odatatype { get; set; } | |
public List<Action> actions { get; set; } | |
public Data() | |
{ | |
odatatype = "microsoft.graph.onTokenIssuanceStartResponseData"; | |
actions = new List<Action>(); | |
actions.Add(new Action()); | |
} | |
} | |
public class Action{ | |
[JsonProperty("@odata.type")] | |
public string odatatype { get; set; } | |
public Claims claims { get; set; } | |
public Action() | |
{ | |
odatatype = "microsoft.graph.tokenIssuanceStart.provideClaimsForToken"; | |
claims = new Claims(); | |
} | |
} | |
public class Claims{ | |
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)] | |
public string CorrelationId { get; set; } | |
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)] | |
public string DateOfBirth { get; set; } | |
public string ApiVersion { get; set; } | |
public List<string> CustomRoles { get; set; } | |
public Claims() | |
{ | |
CustomRoles = new List<string>(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment