-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartupExtension.cs
99 lines (90 loc) · 3.57 KB
/
StartupExtension.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
using GoalTracker.Repository;
using Swashbuckle.AspNetCore.SwaggerUI;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using GoalTracker.Repository.Repos;
using GoalTracker.Services.Interfaces;
using GoalTracker.Services.Services;
using GoalTracker.Domain.Interfaces;
using GoalTracker.Services.Security;
using GoalTracker.API.Helpers.Security;
using System.Text;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
namespace GoalTracker.API
{
public static class StartupExtension
{
public static IServiceCollection AddAzureDataBaseConfiguration(this IServiceCollection services, IConfiguration configuration)
{
var connString = configuration.GetConnectionString("AzureConnection");
services.AddDbContext<DataContext>(options => options.UseSqlServer(connString));
return services;
}
public static IServiceCollection AddCorsForAllOrigins(this IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("AllowAllOrigins", builder =>
builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
return services;
}
public static IServiceCollection AddSwaggerDocumentation(this IServiceCollection services)
{
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1.0", new OpenApiInfo { Title = "Main API v1.0", Version = "v1.0" });
});
return services;
}
public static IApplicationBuilder UseSwaggerDocumentation(this IApplicationBuilder app)
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1.0/swagger.json", "Versioned API v1.0");
c.DocumentTitle = "Title Documentation";
c.DocExpansion(DocExpansion.None);
});
return app;
}
public static IServiceCollection AddApplicationServices(this IServiceCollection services)
{
services.AddScoped<IUserService, UserServices>();
services.AddScoped<IUserRepository, UserRepository>();
services.AddScoped<ISecurePassword, SecurePassword>();
services.AddScoped<Security>();
return services;
}
public static IServiceCollection AddJwTtoken(this IServiceCollection services, IConfiguration configuration)
{
var key = Encoding.ASCII.GetBytes(configuration.GetSection("AppSettings:Token").Value);
services.AddAuthentication(x =>
{
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(x =>
{
x.RequireHttpsMetadata = false;
x.SaveToken = true;
x.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key),
ValidateIssuer = false,
ValidateAudience = false
};
});
return services;
}
}
}