WebFileServer20

Добавлен модуль документации Swashbuckle (swagger) Инструкция

11/4/2020 1:49:02 PM

Details

diff --git "a/Docs/Swagger \320\270\320\275\321\201\321\202\321\200\321\203\320\272\321\206\320\270\321\217.txt" "b/Docs/Swagger \320\270\320\275\321\201\321\202\321\200\321\203\320\272\321\206\320\270\321\217.txt"
new file mode 100644
index 0000000..1b38e5d
--- /dev/null
+++ "b/Docs/Swagger \320\270\320\275\321\201\321\202\321\200\321\203\320\272\321\206\320\270\321\217.txt"
@@ -0,0 +1,7 @@
+1) Авторизоваться в основном приложении
+2) Открыть консоль разработчика
+3) Блок локального хранилища, элемент:
+	Key: WebFileServ.WebAppuser:https://localhost:44303:WebFileServ.WebApp
+	Взять из json value ключ access_token
+4) На странице SwaggerUI в блоке Authorize
+указать строку в формате "Bearer access_token"
\ No newline at end of file
diff --git a/Src/WebFileServ.WebApp/ClientApp/src/App.js b/Src/WebFileServ.WebApp/ClientApp/src/App.js
index 6cc38f2..9bcaf45 100644
--- a/Src/WebFileServ.WebApp/ClientApp/src/App.js
+++ b/Src/WebFileServ.WebApp/ClientApp/src/App.js
@@ -21,6 +21,14 @@ export default class App extends Component {
                 <Route path='/counter' component={CounterPage} />
                 <AuthorizeRoute path='/fetch-data' component={FetchDataPage} />
                 <AuthorizeRoute path='/userPage' component={UserPage} />
+                <Route path='/swagger' component={
+                    () =>
+                    {
+                        window.location.href = '/swagger/index.html';
+                        return null;
+                    }
+                } />
+
                 <Route path={ApplicationPaths.ApiAuthorizationPrefix} component={ApiAuthorizationRoutes} />
             </Layout>
         );
diff --git a/Src/WebFileServ.WebApp/ClientApp/src/Layout/NavMenu.js b/Src/WebFileServ.WebApp/ClientApp/src/Layout/NavMenu.js
index 03c163d..608b86e 100644
--- a/Src/WebFileServ.WebApp/ClientApp/src/Layout/NavMenu.js
+++ b/Src/WebFileServ.WebApp/ClientApp/src/Layout/NavMenu.js
@@ -40,6 +40,9 @@ export class NavMenu extends Component {
                                 <NavItem>
                                     <NavLink tag={Link} className="text-dark" to="/fetch-data">Fetch data</NavLink>
                                 </NavItem>
+                                <NavItem>
+                                    <NavLink tag={Link} className="text-dark" to="/swagger">Swagger</NavLink>
+                                </NavItem>
                                 <LoginMenu>
                                 </LoginMenu>
                             </ul>
diff --git a/Src/WebFileServ.WebApp/Startup.cs b/Src/WebFileServ.WebApp/Startup.cs
index 40c11a5..7a5285f 100644
--- a/Src/WebFileServ.WebApp/Startup.cs
+++ b/Src/WebFileServ.WebApp/Startup.cs
@@ -1,4 +1,5 @@
 using System.Threading.Tasks;
+using System.Collections.Generic;
 
 using Microsoft.AspNetCore.Authentication;
 using Microsoft.AspNetCore.Builder;
@@ -11,6 +12,11 @@ using Microsoft.EntityFrameworkCore;
 using Microsoft.Extensions.Configuration;
 using Microsoft.Extensions.DependencyInjection;
 using Microsoft.Extensions.Hosting;
+using Microsoft.OpenApi.Models;
+
+using Swashbuckle;
+using Swashbuckle.AspNetCore;
+using Swashbuckle.AspNetCore.Swagger;
 
 using WebFileServ.Model.DI;
 
@@ -37,7 +43,6 @@ namespace WebFileServ.WebApp
                 .Registry(services, Configuration);
 
 
-
             services.AddAuthentication()
                 .AddIdentityServerJwt();
 
@@ -45,8 +50,7 @@ namespace WebFileServ.WebApp
             services.AddControllersWithViews();
             services.AddRazorPages();
 
-
-
+            ConfigureServicesSwagger(services);
 
             // In production, the React files will be served from this directory
             services.AddSpaStaticFiles(configuration =>
@@ -55,6 +59,54 @@ namespace WebFileServ.WebApp
             });
         }
 
+        /// <summary>
+        /// https://docs.microsoft.com/ru-ru/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-3.1&tabs=visual-studio
+        /// https://www.thecodebuzz.com/jwt-authorization-token-swagger-open-api-asp-net-core-3-0/
+        /// </summary>
+        /// <param name="services"></param>
+        private void ConfigureServicesSwagger(IServiceCollection services) 
+        {
+            // Register the Swagger generator, defining 1 or more Swagger documents
+            //services.AddSwaggerGen();
+            services.AddSwaggerGen(c =>
+            {
+                c.SwaggerDoc(
+                    "v1",
+                    new OpenApiInfo
+                    {
+                        Title = "TheCodeBuzz-Service",
+                        Version = "v1"
+                    }
+                    );
+
+                c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
+                {
+                    Name = "Authorization",
+                    Type = SecuritySchemeType.ApiKey,
+                    Scheme = "Bearer",
+                    BearerFormat = "JWT",
+                    In = ParameterLocation.Header,
+                    Description = "JWT Authorization header using the Bearer scheme."
+                });
+
+                c.AddSecurityRequirement(new OpenApiSecurityRequirement()
+                {
+                    {
+                        new OpenApiSecurityScheme
+                        {
+                            Reference = new OpenApiReference
+                            {
+                                Type = ReferenceType.SecurityScheme,
+                                Id = "Bearer"
+                            }
+                        },
+                        new string[] {}
+                    }
+                });
+            });
+        }
+
+
         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
         public void Configure(IApplicationBuilder app, IWebHostEnvironment env, DataInitializer dataInitializer)
         {
@@ -89,6 +141,19 @@ namespace WebFileServ.WebApp
                 endpoints.MapRazorPages();
             });
 
+
+            // Enable middleware to serve generated Swagger as a JSON endpoint.
+            app.UseSwagger();
+            // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
+            // specifying the Swagger JSON endpoint.
+            app.UseSwaggerUI(
+                c =>
+                {
+                    c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");                    
+                }
+                );
+
+
             app.UseSpa(spa =>
             {
                 spa.Options.SourcePath = "ClientApp";
@@ -99,7 +164,6 @@ namespace WebFileServ.WebApp
                 }
             });
 
-
             //������������ ������
             //�� ������ ������ ������ ��
             using (dataInitializer)
@@ -111,5 +175,8 @@ namespace WebFileServ.WebApp
                     .GetResult();
             }
         }
+
+
+
     }
 }
diff --git a/Src/WebFileServ.WebApp/WebFileServ.WebApp.csproj b/Src/WebFileServ.WebApp/WebFileServ.WebApp.csproj
index d3d9df0..7341d7b 100644
--- a/Src/WebFileServ.WebApp/WebFileServ.WebApp.csproj
+++ b/Src/WebFileServ.WebApp/WebFileServ.WebApp.csproj
@@ -22,6 +22,7 @@
     <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.9" />
     <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.9" />
     <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.4" />
+    <PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
   </ItemGroup>
 
   <ItemGroup>