131 lines
4.9 KiB
C#
131 lines
4.9 KiB
C#
using KTUSAPS.Auth;
|
||
using KTUSAPS.Services;
|
||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||
using Microsoft.AspNetCore.Authorization;
|
||
using Microsoft.AspNetCore.Builder;
|
||
using Microsoft.AspNetCore.Hosting;
|
||
using Microsoft.EntityFrameworkCore;
|
||
using Microsoft.Extensions.Configuration;
|
||
using Microsoft.Extensions.DependencyInjection;
|
||
using Microsoft.Extensions.Hosting;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using VueCliMiddleware;
|
||
|
||
namespace KTUSAPS
|
||
{
|
||
public class Startup
|
||
{
|
||
public Startup(IConfiguration configuration)
|
||
{
|
||
Configuration = configuration;
|
||
}
|
||
|
||
public IConfiguration Configuration { get; }
|
||
|
||
// This method gets called by the runtime. Use this method to add services to the container.
|
||
public void ConfigureServices(IServiceCollection services)
|
||
{
|
||
services.AddControllers(options =>
|
||
options.SuppressAsyncSuffixInActionNames = false
|
||
)
|
||
.AddControllersAsServices();
|
||
services.AddSpaStaticFiles(configuration =>
|
||
{
|
||
configuration.RootPath = "ClientApp/dist";
|
||
});
|
||
|
||
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||
.AddJwtBearer(options =>
|
||
{
|
||
options.MetadataAddress = "https://login.microsoftonline.com/3415f2f7-f5a8-4092-b52a-003aaf844853/v2.0/.well-known/openid-configuration";
|
||
options.Audience = Configuration["ClientId"];
|
||
//options.Authority = Configuration["Authority"];
|
||
});
|
||
|
||
services.AddAuthorization((configure) =>
|
||
{
|
||
var adminPolicy = new Microsoft.AspNetCore.Authorization.AuthorizationPolicyBuilder()
|
||
.RequireAuthenticatedUser()
|
||
.AddRequirements(new AdminRequirement())
|
||
.Build();
|
||
configure.AddPolicy("admin", adminPolicy);
|
||
configure.DefaultPolicy = new Microsoft.AspNetCore.Authorization.AuthorizationPolicyBuilder()
|
||
.RequireAuthenticatedUser()
|
||
.Build();
|
||
});
|
||
|
||
var connectionString = Configuration.GetConnectionString("Main");
|
||
services.AddDbContext<Data.SAPSDataContext>((options) => options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString)));
|
||
services.AddHostedService<DatabaseInitializationService>();
|
||
|
||
services.AddSingleton<IAuthorizationHandler, AdminAuthorizationHandler>();
|
||
|
||
services.AddSwaggerGen(options =>
|
||
{
|
||
options.OperationFilter<AuthorizeCheckOperationFilter>();
|
||
options.AddSecurityDefinition("msad", new Microsoft.OpenApi.Models.OpenApiSecurityScheme()
|
||
{
|
||
Type = Microsoft.OpenApi.Models.SecuritySchemeType.OAuth2,
|
||
Flows = new Microsoft.OpenApi.Models.OpenApiOAuthFlows()
|
||
{
|
||
AuthorizationCode = new Microsoft.OpenApi.Models.OpenApiOAuthFlow()
|
||
{
|
||
AuthorizationUrl = new Uri("https://login.microsoftonline.com/3415f2f7-f5a8-4092-b52a-003aaf844853/oauth2/v2.0/authorize"),
|
||
TokenUrl = new Uri("https://login.microsoftonline.com/3415f2f7-f5a8-4092-b52a-003aaf844853/oauth2/v2.0/token"),
|
||
Scopes = new Dictionary<string, string>
|
||
{
|
||
{ "openid", "Access to user's id" },
|
||
{ "profile", "Access to user's name" },
|
||
{ "email", "Access to email" }
|
||
}
|
||
}
|
||
}
|
||
});
|
||
});
|
||
}
|
||
|
||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||
{
|
||
if (env.IsDevelopment())
|
||
{
|
||
app.UseDeveloperExceptionPage();
|
||
|
||
app.UseSwaggerUI(c =>
|
||
{
|
||
c.SwaggerEndpoint("/swagger/v1/swagger.json", "API");
|
||
|
||
c.OAuthClientId(Configuration["ClientId"]);
|
||
c.OAuthAppName("KTUSA Problem<65> sistema");
|
||
c.OAuthUsePkce();
|
||
});
|
||
}
|
||
|
||
app.UseSwagger(c =>
|
||
{
|
||
});
|
||
app.UseRouting();
|
||
app.UseSpaStaticFiles();
|
||
app.UseAuthentication();
|
||
app.UseAuthorization();
|
||
|
||
app.UseEndpoints(endpoints =>
|
||
{
|
||
endpoints.MapControllers();
|
||
});
|
||
|
||
app.UseSpa(spa =>
|
||
{
|
||
spa.Options.SourcePath = "ClientApp/";
|
||
|
||
if (env.IsDevelopment())
|
||
{
|
||
spa.UseVueCli(npmScript: "serve");
|
||
}
|
||
|
||
});
|
||
}
|
||
}
|
||
}
|