2017-04-17 6 views
11

을 사용하여 Swagger (ASP.NET Core) 설정하기 나는 웹 API (ASP.NET Core)를 가지고 있으며, 그것을 호출하기 위해 swagger를 조정하려고합니다. 호출에는 Authorization 헤더가 있어야하며 Bearer 인증을 사용하고 있습니다. 우편 배달부 등의 타사 앱에서 걸려 오는 전화는 문제가 없습니다. 하지만 (내가 헤더를받지 못하는 이유 때문에) 허풍에 대한 헤더를 설정하는 데 문제가 있습니다. 이것은 지금과 같은 모습입니다 : 모든권한 부여 헤더 (무기명)

"host": "localhost:50352", 
    "basePath": "/" , 
    "schemes": [ 
    "http", 
    "https" 
    ], 
"securityDefinitions": { 
    "Bearer": { 
     "name": "Authorization", 
     "in": "header", 
     "type": "apiKey", 
     "description": "HTTP/HTTPS Bearer" 
    } 
    }, 
    "paths": { 
    "/v1/{subAccountId}/test1": { 
     "post": { 
     "tags": [ 
      "auth" 
     ], 
     "operationId": "op1", 
     "consumes": ["application/json", "application/html"], 
     "produces": ["application/json", "application/html"], 
     "parameters": [ 
      { 
      "name": "subAccountId", 
      "in": "path", 
      "required": true, 
      "type": "string" 
      } 
     ], 
     "security":[{ 
      "Bearer": [] 
     }], 
     "responses": { 
      "204": { 
      "description": "No Content" 
      }, 
      "400": { 
      "description": "BadRequest", 
      "schema": { 
       "$ref": "#/definitions/ErrorResponse" 
      } 
      }, 
      "401": { 
      "description": "Unauthorized", 
      "schema": { 
       "$ref": "#/definitions/ErrorResponse" 
      } 
      }, 
      "500": { 
      "description": "InternalServerError", 
      "schema": { 
       "$ref": "#/definitions/ErrorResponse" 
      } 
      } 
     }, 
     "deprecated": false 
     } 
    }, 

답변

21

첫째, 당신은 당신의 자신감 정의를 생성하는 자동차에 대한 Swashbuckle.AspNetCore nuget 패키지를 사용할 수 있습니다. 당신이 패키지를 설치 한 후

, 방법 ConfigureServices에서 Startup.cs에서 설정을

services.AddSwaggerGen(c => { 
c.AddSecurityDefinition("Bearer", new ApiKeyScheme() { In = "header", Description = "Please insert JWT with Bearer into field", Name = "Authorization", Type = "apiKey" }); 

그런 다음 당신은 페이지의 오른쪽 상단에 인증 단추를 사용할 수 있습니다.

은 적어도 당신은 현재 자신감이 JWT 토큰과 인증 기능이 자동으로 헤더에 토큰을 추가 할 수 있습니다

+0

당신의 설명이 나를 위해 문제를 해결했습니다. –

+0

좋은 답변이지만 ABP 상용구를 사용하면 동적 웹 API (https://aspnetboilerplate.com/Pages/Documents/Dynamic-Web-API) –

+2

@ VadimK와 함께 작동하지 않습니다. 업그레이드 할 때까지 완벽하게 작동했습니다. .NET Core 2.0 – monty

1

, 나는 here 대답 한 유효 자신감 정의를 생성하려면이 패키지를 사용하려고 할 수 있습니다.

관련 문제