-
Notifications
You must be signed in to change notification settings - Fork 3
/
Startup.cs
54 lines (44 loc) · 2.14 KB
/
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using Microsoft.Identity.Web;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Azure.WebJobs.Host.Bindings;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
[assembly: FunctionsStartup(typeof(SecureAzureFunctionMiW.Startup))]
namespace SecureAzureFunctionMiW
{
public class Startup : FunctionsStartup
{
public Startup()
{
}
IConfiguration Configuration { get; set; }
public override void Configure(IFunctionsHostBuilder builder)
{
// Get the azure function application directory. 'C:\whatever' for local and 'd:\home\whatever' for Azure
var executionContextOptions = builder.Services.BuildServiceProvider()
.GetService<IOptions<ExecutionContextOptions>>().Value;
var currentDirectory = executionContextOptions.AppDirectory;
// Get the original configuration provider from the Azure Function
var configuration = builder.Services.BuildServiceProvider().GetService<IConfiguration>();
// Create a new IConfigurationRoot and add our configuration along with Azure's original configuration
Configuration = new ConfigurationBuilder()
.SetBasePath(currentDirectory)
.AddConfiguration(configuration) // Add the original function configuration
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
// Replace the Azure Function configuration with our new one
builder.Services.AddSingleton(Configuration);
ConfigureServices(builder.Services);
}
private void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(sharedOptions =>
{
sharedOptions.DefaultScheme = Microsoft.Identity.Web.Constants.Bearer;
sharedOptions.DefaultChallengeScheme = Microsoft.Identity.Web.Constants.Bearer;
})
.AddMicrosoftIdentityWebApi(Configuration.GetSection("AzureAd"));
}
}
}