Created
November 25, 2017 03:41
-
-
Save bcnzer/44dfea610975780abd6b7277eb040138 to your computer and use it in GitHub Desktop.
Simple Azure Function based on the HTTP Trigger sample code, with the addition of the token validation call
This file contains hidden or 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
| using System; | |
| using System.IdentityModel.Tokens.Jwt; | |
| using System.Linq; | |
| using System.Net; | |
| using System.Net.Http; | |
| using System.Security.Claims; | |
| using System.Threading.Tasks; | |
| using Microsoft.Azure.WebJobs; | |
| using Microsoft.Azure.WebJobs.Extensions.Http; | |
| using Microsoft.Azure.WebJobs.Host; | |
| namespace SpotterPowerlifting.Functions | |
| { | |
| public static class AuthTest | |
| { | |
| [FunctionName("AuthTest")] | |
| public static async Task<HttpResponseMessage> Run( | |
| [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log) | |
| { | |
| log.Info("C# HTTP trigger function processed a request."); | |
| ClaimsPrincipal principal; | |
| if ((principal = await Security.ValidateTokenAsync(req.Headers.Authorization)) == null) | |
| { | |
| return req.CreateResponse(HttpStatusCode.Unauthorized); | |
| } | |
| // parse query parameter | |
| string name = req.GetQueryNameValuePairs() | |
| .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0) | |
| .Value; | |
| // Get request body | |
| dynamic data = await req.Content.ReadAsAsync<object>(); | |
| // Set name to query string or body data | |
| name = name ?? data?.name; | |
| return name == null | |
| ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body") | |
| : req.CreateResponse(HttpStatusCode.OK, "Hello " + name); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What packages do you need for this? Because as a
csxfunc it's failing to recogniseSecurity.ValidateTokenAsync()and i can't seem to make it work in anetcoreapp2.1proj app either?