· 6 years ago · Dec 03, 2019, 10:30 AM
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5using System.Reflection;
6using System.Text;
7using System.Threading.Tasks;
8using AutoMapper;
9using Microsoft.AspNetCore.Authentication.JwtBearer;
10using Microsoft.AspNetCore.Builder;
11using Microsoft.AspNetCore.Hosting;
12using Microsoft.AspNetCore.Mvc;
13using Microsoft.Extensions.Configuration;
14using Microsoft.Extensions.DependencyInjection;
15using Microsoft.IdentityModel.Tokens;
16using Swashbuckle.AspNetCore.Swagger;
17using Microsoft.EntityFrameworkCore;
18using e_trust_API.Models;
19using e_trust_API.Filters;
20using Stripe;
21using Microsoft.AspNetCore.Http.Features;
22
23namespace e_trust_API
24{
25 public class Startup
26 {
27 public IConfigurationRoot Configuration{get;set;}
28
29 public static string ConnectionString{get;private set;}
30 public static string Issuer { get; private set; }
31 public static string Audience { get; private set; }
32 public static string Key { get; private set; }
33 public static string GoogleAPIKey { get; private set; }
34 public static string ContentRootPath { get; private set; }
35 public static string SiteUrl { get; set; }
36 public static string EmailRegistration { get; set; }
37 public static string CompanyId { get; set; }
38 public static string MemberId { get; set; }
39 public static string HyperAdminId { get; set; }
40 public static string MemberType { get; set; }
41 public static string FirstName { get; set; }
42 public static string LastName { get; set; }
43 public static string Email { get; set; }
44 public static string Latitude { get; set; }
45 public static string Longitude { get; set; }
46 public static string emptyavatar { get; private set; }
47
48
49 private readonly IHostingEnvironment _hostingEnvironment;
50
51 public Startup(IHostingEnvironment env)
52 {
53 _hostingEnvironment = env;
54 Configuration = new ConfigurationBuilder().SetBasePath(env.ContentRootPath).AddJsonFile("appSettings.json").Build();
55
56 #if DEBUG
57 StripeConfiguration.SetApiKey(Configuration["Stripe:TestKeys:SecretKey"]);
58 #elif RELEASE
59 // we will use the TestKeys until further notice
60 StripeConfiguration.SetApiKey(Configuration["Stripe:TestKeys:SecretKey"]);
61 #endif
62 }
63
64 //public Startup(IConfiguration configuration)
65 //{
66 // Configuration = configuration;
67 //}
68 //public IConfiguration Configuration { get; }
69
70 // This method gets called by the runtime. Use this method to add services to the container.
71
72 public void ConfigureServices(IServiceCollection services)
73 {
74 //services.AddJwtBearerAuthentication(Configuration);
75
76 services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
77 {
78 options.TokenValidationParameters = new TokenValidationParameters
79 {
80 ValidateIssuer = true,
81 ValidateAudience = true,
82 ValidateIssuerSigningKey = true,
83 ValidIssuer = Startup.Issuer,
84 ValidAudience = Startup.Audience,
85 IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Startup.Key))
86 };
87 });
88
89 services.AddCors(options =>
90 {
91 options.AddPolicy("AllowAll",
92 builder =>
93 {
94 builder
95 .AllowAnyOrigin()
96 .AllowAnyMethod()
97 .AllowAnyHeader()
98 .AllowCredentials();
99 });
100 });
101
102 services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
103
104 //To check before execution of action
105 //services.AddMvc()
106 // .AddMvcOptions(options =>
107 // {
108 // options.Filters.Add<ActionFilters>();
109 // });
110 //services.AddSingleton<ActionFilters>();
111 //end
112
113 var connection = Configuration["ConnectionStrings:DefaultConnection"];
114
115 services.AddDbContext<ETrustContext>
116 (options => options.UseSqlServer(connection));
117
118 services.AddAutoMapper();
119
120 //services.AddSwaggerGen(c =>
121 //{
122 // c.SwaggerDoc("v1", new Info { Title = "MY API", Version = "v1" });
123 //});
124
125 // Register the Swagger generator, defining 1 or more Swagger documents
126 services.AddSwaggerGen(c =>
127 {
128 c.SwaggerDoc("v1", new Info
129 {
130 Version = "v1",
131 Title = "Equanet DEV API",
132 Description = "Equanet DEV API for development purpouses",
133
134 License = new License
135 {
136 Name = "Use under LICX",
137 Url = "https://example.com/license"
138 },
139 });
140
141 var security = new Dictionary<string, IEnumerable<string>>
142 {
143 {"Bearer", new string[] { }},
144 };
145
146 c.AddSecurityDefinition("Bearer", new ApiKeyScheme
147 {
148 Description = "JWT Authorization header using the Bearer scheme. Example: \"authtoken: Bearer {token}\"",
149 Name = "authtoken",
150 In = "header",
151 Type = "apiKey"
152 });
153 c.AddSecurityRequirement(security);
154
155
156 // Set the comments path for the Swagger JSON and UI.
157 var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
158 var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
159 c.IncludeXmlComments(xmlPath);
160 });
161 }
162
163 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
164 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
165 {
166 if (env.IsDevelopment())
167 {
168 app.UseDeveloperExceptionPage();
169 }
170 app.UseMiddleware<ErrorHandlingMiddleware>();
171 ConnectionString = Configuration["ConnectionStrings:DefaultConnection"];
172 Issuer = Configuration["Issuer"];
173 Audience = Configuration["Audience"];
174 Key = Configuration["Key"];
175 GoogleAPIKey= Configuration["GoogleAPIKey"];
176 ContentRootPath = _hostingEnvironment.ContentRootPath;
177 SiteUrl= Configuration["SiteUrl"];
178 EmailRegistration= Configuration["EmailRegistration"];
179 emptyavatar = Configuration["emptyavatar"];
180 app.UseAuthentication();
181 // Enable middleware to serve generated Swagger as a JSON endpoint.
182 app.UseSwagger();
183 // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
184 // specifying the Swagger JSON endpoint.
185 app.UseSwaggerUI(c =>
186 {
187 c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
188 });
189 app.UseCors("AllowAll");
190 app.UseMvc();
191 }
192 }
193}