Skip to content

Instantly share code, notes, and snippets.

@ahelland
Created September 11, 2018 10:37
Show Gist options
  • Save ahelland/7a07c76436e87681667a769eaa296a18 to your computer and use it in GitHub Desktop.
Save ahelland/7a07c76436e87681667a769eaa296a18 to your computer and use it in GitHub Desktop.
Implementing Multiple Identities in your .NET Core Web App – Part 2 - Azure Function for checking Partner role
#r "Newtonsoft.Json"
using System;
using System.Net;
using System.Net.Http.Formatting;
using Newtonsoft.Json;
public static async Task<object> Run(HttpRequestMessage request, TraceWriter log)
{
log.Info($"Webhook was triggered!");
string requestContentAsString = await request.Content.ReadAsStringAsync();
dynamic requestContentAsJObject = JsonConvert.DeserializeObject(requestContentAsString);
if (requestContentAsJObject.emailAddress == null)
{
return request.CreateResponse(HttpStatusCode.BadRequest);
}
var email = ((string) requestContentAsJObject.emailAddress).ToLower();
var role = ((string) requestContentAsJObject.role).ToLower();
log.Info($"email: {email}, role: {role}");
char splitter = '@';
string[] splitEmail = email.Split(splitter);
var emailSuffix = splitEmail[1];
if ((role == "partner" && email != "[email protected]") || (role == "partner" && emailSuffix != "northwind.com"))
{
return request.CreateResponse<ResponseContent>(
HttpStatusCode.Conflict,
new ResponseContent
{
version = "1.0.0",
status = (int) HttpStatusCode.Conflict,
userMessage = $"Your account does not seem to be a partner account."
},
new JsonMediaTypeFormatter(),
"application/json");
}
return request.CreateResponse(HttpStatusCode.OK);
}
public class ResponseContent
{
public string version { get; set; }
public int status { get; set; }
public string userMessage { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment