+
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
namespace ApplicationSystem.Configurations.Configurations
|
||||
{
|
||||
public class SwaggerConfiguration
|
||||
{
|
||||
public bool UseSwagger { get; init; } = true;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using ApplicationSystem.MediatRHelper.Models;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace ApplicationSystem.Contracts.Models.UserRequests
|
||||
{
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.10" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.17" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -6,12 +6,13 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.16" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.10" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.16">
|
||||
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.17" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.17" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.17">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.Identity.Core" Version="8.0.17" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -3,6 +3,7 @@ using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Options;
|
||||
using ApplicationSystem.Configurations.Configurations;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace ApplicationSystem.Identity.Extensions
|
||||
{
|
||||
@@ -16,9 +17,12 @@ namespace ApplicationSystem.Identity.Extensions
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
/// <returns></returns>
|
||||
public static IServiceCollection AddApplicationSystemIdentityDatabase(this IServiceCollection services)
|
||||
public static IServiceCollection AddApplicationSystemIdentityDatabase(
|
||||
this IServiceCollection services,
|
||||
IConfiguration configuration)
|
||||
{
|
||||
services.AddOptions<ConnectionStringsConfiguration>();
|
||||
services.Configure<ConnectionStringsConfiguration>(
|
||||
configuration.GetSection(nameof(ConnectionStringsConfiguration)));
|
||||
|
||||
services.AddDbContext<IdentityContext>((sp, opt) =>
|
||||
{
|
||||
|
||||
@@ -9,11 +9,14 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
|
||||
<PackageReference Include="MediatR" Version="8.1.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.16">
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.17" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.17">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.Identity.Core" Version="8.0.17" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="8.1.4" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="8.1.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
using Asp.Versioning;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace ApplicationSystem.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
[ApiVersion("1.0")]
|
||||
[Route("api/v{version:apiVersion}/[controller]")]
|
||||
public class ApplicationsController : ControllerBase
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Asp.Versioning;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace ApplicationSystem.Controllers
|
||||
@@ -9,6 +10,7 @@ namespace ApplicationSystem.Controllers
|
||||
public class UsersController : ControllerBase
|
||||
{
|
||||
[HttpPost("login")]
|
||||
[ProducesResponseType<IdentityResult>(200)]
|
||||
public async Task<IActionResult> LoginAsync()
|
||||
{
|
||||
await Task.Delay(100);
|
||||
|
||||
27
ApplicationSystem/Extensions/ApplicationExtensions.cs
Normal file
27
ApplicationSystem/Extensions/ApplicationExtensions.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using ApplicationSystem.Configurations.Configurations;
|
||||
using ApplicationSystem.Identity.Database.Models;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace ApplicationSystem.Extensions
|
||||
{
|
||||
public static class ApplicationExtensions
|
||||
{
|
||||
public static void UseApplicationSwagger(this WebApplication applicationBuilder)
|
||||
{
|
||||
var options = applicationBuilder.Services
|
||||
.GetRequiredService<IOptions<SwaggerConfiguration>>()
|
||||
.Value;
|
||||
|
||||
if (options.UseSwagger)
|
||||
{
|
||||
applicationBuilder.UseSwagger();
|
||||
applicationBuilder.UseSwaggerUI();
|
||||
}
|
||||
}
|
||||
|
||||
public static void MapIdentityApi(this WebApplication applicationBuilder)
|
||||
{
|
||||
applicationBuilder.MapIdentityApi<ApplicationSystemUser>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,10 @@
|
||||
using ApplicationSystem.Identity.Database.Context;
|
||||
using ApplicationSystem.Identity.Database.Models;
|
||||
using ApplicationSystem.Identity.Extensions;
|
||||
using Asp.Versioning;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.OpenApi.Models;
|
||||
|
||||
namespace ApplicationSystem.Extensions
|
||||
{
|
||||
@@ -10,20 +13,76 @@ namespace ApplicationSystem.Extensions
|
||||
/// </summary>
|
||||
public static class DiExtensions
|
||||
{
|
||||
public static IServiceCollection AddSwagger(this IServiceCollection services)
|
||||
{
|
||||
_ = services.AddApiVersioning(setup =>
|
||||
{
|
||||
setup.DefaultApiVersion = new ApiVersion(1, 0);
|
||||
setup.AssumeDefaultVersionWhenUnspecified = true;
|
||||
setup.ReportApiVersions = true;
|
||||
}).AddApiExplorer(setup =>
|
||||
{
|
||||
setup.GroupNameFormat = "'v'VVV";
|
||||
setup.SubstituteApiVersionInUrl = true;
|
||||
setup.DefaultApiVersion = new ApiVersion(1, 0);
|
||||
});
|
||||
|
||||
_ = services.AddSwaggerGen(opt =>
|
||||
{
|
||||
opt.SwaggerDoc("v1", new OpenApiInfo { Title = "ApplicationSystem", Version = "1", });
|
||||
opt.EnableAnnotations();
|
||||
opt.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
|
||||
{
|
||||
Name = "Authorization",
|
||||
Type = SecuritySchemeType.ApiKey,
|
||||
Scheme = "Bearer",
|
||||
BearerFormat = "JWT",
|
||||
In = ParameterLocation.Header,
|
||||
Description = "JWT Authorization header using the Bearer scheme."
|
||||
});
|
||||
|
||||
opt.AddSecurityRequirement(new OpenApiSecurityRequirement
|
||||
{
|
||||
{
|
||||
new OpenApiSecurityScheme
|
||||
{
|
||||
Reference = new OpenApiReference
|
||||
{
|
||||
Type = ReferenceType.SecurityScheme,
|
||||
Id = "Bearer"
|
||||
},
|
||||
Scheme = IdentityConstants.BearerScheme,
|
||||
Name = "Bearer",
|
||||
In = ParameterLocation.Header
|
||||
},
|
||||
Array.Empty<string>()
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Добавить ролевую систему
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
/// <returns></returns>
|
||||
public static IServiceCollection AddApplicationSystemIdentity(this IServiceCollection services)
|
||||
public static IServiceCollection AddApplicationSystemIdentity(
|
||||
this IServiceCollection services,
|
||||
IConfiguration configuration)
|
||||
{
|
||||
services.AddAuthentication();
|
||||
services.AddAuthorization();
|
||||
services.AddAuthentication()
|
||||
.AddCookie(IdentityConstants.ApplicationScheme)
|
||||
.AddBearerToken(IdentityConstants.BearerScheme);
|
||||
|
||||
services.AddApplicationSystemIdentityDatabase();
|
||||
services.AddApplicationSystemIdentityDatabase(configuration);
|
||||
|
||||
services.AddIdentity<ApplicationSystemUser, ApplicationSystemRole>()
|
||||
services.AddIdentityCore<ApplicationSystemUser>()
|
||||
.AddRoles<ApplicationSystemRole>()
|
||||
.AddEntityFrameworkStores<IdentityContext>()
|
||||
.AddApiEndpoints()
|
||||
.AddDefaultTokenProviders();
|
||||
|
||||
return services;
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace ApplicationSystem.Models.Queries.AplicationQueries
|
||||
{
|
||||
public class ApplicationCreateCommand
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,18 @@
|
||||
using ApplicationSystem.Extensions;
|
||||
using ApplicationSystem.Identity.Database.Models;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Services.AddApplicationSystemIdentity();
|
||||
builder.Services.AddControllers().AddNewtonsoftJson();
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
|
||||
builder.Services.AddSwagger();
|
||||
|
||||
builder.Services.AddApplicationSystemIdentity(builder.Configuration);
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
app.MapIdentityApi<ApplicationSystemUser>();
|
||||
app.UseApplicationSwagger();
|
||||
|
||||
app.Run();
|
||||
|
||||
@@ -1,18 +1,11 @@
|
||||
{
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:4737",
|
||||
"sslPort": 44306
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "http://localhost:5053",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
@@ -22,14 +15,8 @@
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"applicationUrl": "https://localhost:7164;http://localhost:5053",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "https://0.0.0.0:7164;http://0.0.0.0:5053",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user