5

I am trying to develop following scenario using Azure functions.

I have developed Asp.Net Web API which handles the Database related operation. Now, I want to implement a scheduler like functionality which will run once a day and will clean up junk data from database. I've created an endpoint for that in my Web API but I want to execute it on regular basis so I think to implement scheduler using Azure function's TimerTrigger function, is there any way to call my web api's endpoint in TimerTrigger function.

How to handle my api's authentication in Azure function?

Thanks

Update:

Based on mikhail's answer, finally I got the token using following code:

var client = new HttpClient();
client.BaseAddress = new Uri(apirooturl);

var grant_type = "password";
var username = "username";
var password = "password";

var formContent = new FormUrlEncodedContent(new[]
{
    new KeyValuePair<string, string>("grant_type", grant_type),
    new KeyValuePair<string, string>("username", username),
    new KeyValuePair<string, string>("password", password)
});

var token = client.PostAsync("token", formContent).Result.Content.ReadAsAsync<AuthenticationToken>().Result;

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(token.token_type, token.access_token);

var response = await client.GetAsync(apiendpoint);
var content = await response.Content.ReadAsStringAsync();
2
  • How/What did you add references so you could use AuthenticationToken? Thanks. Commented Oct 30, 2017 at 12:36
  • @SkorunkaFrantišek i added using System.Net.Http; as it's a part of DefaultRequestHeaders property of HttpClient class and we can find HttpClient under System.Net.Http namespace. Commented Oct 31, 2017 at 16:58

2 Answers 2

5

Azure Function is running in a normal Web App, so you can do pretty much anything there. Assuming you are on C#, the function body might looks something like

var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = 
    new AuthenticationHeaderValue("Bearer", token);

var response = await client.GetAsync(url);
var content = await response.Content.ReadAsStringAsync();
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for quick answer. I am just wondering how to get token, as my api will be on different url. Also, I tried to call my token endpoint but it is not returning the token.
I gave Bearer authentication as just an example. It depends on what you configured for your API. If you have authentication working somewhere, you'll be able to copy it to Function App.
I'll take a look. Thanks a lot.
0

You may be better off putting the entire database cleanup logic in the function and making it timer triggered, that way you keep your API out of it altogether.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.