forked from Azure/Commercial-Marketplace-SaaS-Accelerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Startup.cs
187 lines (171 loc) · 8.54 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for license information.
namespace Microsoft.Marketplace.Saas.Web
{
using global::Azure.Identity;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using Microsoft.Marketplace.Metering;
using Microsoft.Marketplace.SaaS;
using Microsoft.Marketplace.SaaS.SDK.Services.Configurations;
using Microsoft.Marketplace.SaaS.SDK.Services.Contracts;
using Microsoft.Marketplace.SaaS.SDK.Services.Models;
using Microsoft.Marketplace.SaaS.SDK.Services.Services;
using Microsoft.Marketplace.SaaS.SDK.Services.Utilities;
using Microsoft.Marketplace.SaasKit.Client.DataAccess.Context;
using Microsoft.Marketplace.SaasKit.Client.DataAccess.Contracts;
using Microsoft.Marketplace.SaasKit.Client.DataAccess.Services;
/// <summary>
/// Startup.
/// </summary>
public class Startup
{
/// <summary>
/// Initializes a new instance of the <see cref="Startup"/> class.
/// </summary>
/// <param name="configuration">The configuration.</param>
public Startup(IConfiguration configuration)
{
this.Configuration = configuration;
}
/// <summary>
/// Gets the configuration.
/// </summary>
/// <value>
/// The configuration.
/// </value>
public IConfiguration Configuration { get; }
/// <summary>
/// Configures the services.
/// </summary>
/// <param name="services">The services.</param>
public void ConfigureServices(IServiceCollection services)
{
var loggerFactory = LoggerFactory.Create(builder =>
{
builder
.AddConsole();
});
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
var config = new SaaSApiClientConfiguration()
{
AdAuthenticationEndPoint = this.Configuration["SaaSApiConfiguration:AdAuthenticationEndPoint"],
ClientId = this.Configuration["SaaSApiConfiguration:ClientId"],
ClientSecret = this.Configuration["SaaSApiConfiguration:ClientSecret"],
FulFillmentAPIBaseURL = this.Configuration["SaaSApiConfiguration:FulFillmentAPIBaseURL"],
MTClientId = this.Configuration["SaaSApiConfiguration:MTClientId"],
FulFillmentAPIVersion = this.Configuration["SaaSApiConfiguration:FulFillmentAPIVersion"],
GrantType = this.Configuration["SaaSApiConfiguration:GrantType"],
Resource = this.Configuration["SaaSApiConfiguration:Resource"],
SaaSAppUrl = this.Configuration["SaaSApiConfiguration:SaaSAppUrl"],
SignedOutRedirectUri = this.Configuration["SaaSApiConfiguration:SignedOutRedirectUri"],
TenantId = this.Configuration["SaaSApiConfiguration:TenantId"],
};
var knownUsers = new KnownUsersModel()
{
KnownUsers = this.Configuration["KnownUsers"],
};
var creds = new ClientSecretCredential(config.TenantId.ToString(), config.ClientId.ToString(), config.ClientSecret);
services
.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = OpenIdConnectDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddOpenIdConnect(options =>
{
options.Authority = $"{config.AdAuthenticationEndPoint}/common/v2.0";
options.ClientId = config.MTClientId;
options.ResponseType = OpenIdConnectResponseType.IdToken;
options.CallbackPath = "/Home/Index";
options.SignedOutRedirectUri = config.SignedOutRedirectUri;
options.TokenValidationParameters.NameClaimType = "name";
options.TokenValidationParameters.ValidateIssuer = false;
})
.AddCookie();
services
.AddTransient<IClaimsTransformation, CustomClaimsTransformation>();
services
.AddSingleton<IFulfillmentApiService>(new FulfillmentApiService(new MarketplaceSaaSClient(creds), config, new FulfillmentApiClientLogger()))
.AddSingleton<IMeteredBillingApiService>(new MeteredBillingApiService(new MarketplaceMeteringClient(creds), config, new MeteringApiClientLogger()))
.AddSingleton<SaaSApiClientConfiguration>(config)
.AddSingleton<KnownUsersModel>(knownUsers);
services
.AddDbContext<SaasKitContext>(options => options.UseSqlServer(this.Configuration.GetConnectionString("DefaultConnection")));
InitializeRepositoryServices(services);
services.AddMvc(option => option.EnableEndpointRouting = false);
services.AddControllersWithViews();
services.Configure<CookieTempDataProviderOptions>(options => {
options.Cookie.IsEssential = true;
});
}
/// <summary>
/// Configures the specified application.
/// </summary>
/// <param name="app">The application.</param>
/// <param name="env">The env.</param>
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
/// <summary>
/// Initializes the repository services.
/// </summary>
/// <param name="services">The services.</param>
private static void InitializeRepositoryServices(IServiceCollection services)
{
services.AddScoped<ISubscriptionsRepository, SubscriptionsRepository>();
services.AddScoped<IPlansRepository, PlansRepository>();
services.AddScoped<IUsersRepository, UsersRepository>();
services.AddScoped<ISubscriptionLogRepository, SubscriptionLogRepository>();
services.AddScoped<IApplicationConfigRepository, ApplicationConfigRepository>();
services.AddScoped<IApplicationLogRepository, ApplicationLogRepository>();
services.AddScoped<ISubscriptionUsageLogsRepository, SubscriptionUsageLogsRepository>();
services.AddScoped<IMeteredDimensionsRepository, MeteredDimensionsRepository>();
services.AddScoped<IKnownUsersRepository, KnownUsersRepository>();
services.AddScoped<IOffersRepository, OffersRepository>();
services.AddScoped<IValueTypesRepository, ValueTypesRepository>();
services.AddScoped<IOfferAttributesRepository, OfferAttributesRepository>();
services.AddScoped<IEmailTemplateRepository, EmailTemplateRepository>();
services.AddScoped<IPlanEventsMappingRepository, PlanEventsMappingRepository>();
services.AddScoped<IEventsRepository, EventsRepository>();
services.AddScoped<KnownUserAttribute>();
services.AddScoped<IEmailService, SMTPEmailService>();
}
}
}