2017-04-17 13 views
11

का उपयोग कर स्वैगर (एएसपी.नेट कोर) सेट अप करना मेरे पास एक वेब एपीआई (एएसपी.नेट कोर) है और मैं इसे कॉल करने के लिए स्वैगर समायोजित करने की कोशिश कर रहा हूं। कॉल में प्राधिकरण शीर्षलेख होना चाहिए और मैं बेयरर प्रमाणीकरण का उपयोग कर रहा हूं। पोस्टमैन आदि जैसे तृतीय पक्ष ऐप्स से कॉल ठीक हो जाते हैं। लेकिन मुझे हेडर्स को स्वैगर के लिए सेट करने में समस्या है (किसी कारण से मुझे हेडर नहीं मिलते हैं)। यह है कि यह कैसे अब की तरह लग रहा है:प्राधिकरण शीर्षलेख (बीयर)

सभी की
"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" }); 

तो फिर तुम पेज के ऊपरी दाएँ भाग में अधिकृत बटन का उपयोग कर सकते हैं।

कम से कम आप इस पैकेज का उपयोग करने उत्पन्न करने के लिए वैध अकड़ परिभाषा

+0

आपकी व्याख्या ने मेरे लिए समस्या हल कर दी है। –

+0

अच्छा जवाब लेकिन जब मैं एबीपी बॉयलरप्लेट के साथ ऐसा करता हूं, तो यह डायनामिक वेब एपी (https://aspnetboilerplate.com/Pages/Documents/Dynamic-Web-API) –

+2

@VadimK के साथ काम नहीं करता है जब तक कि मैं अपग्रेड नहीं करता .NET कोर 2.0 – monty

1

वर्तमान में स्वैगर जेडब्ल्यूटी-टोकन के माध्यम से प्रमाणीकरण के लिए कार्यक्षमता है और स्वचालित रूप से शीर्ष लेख में टोकन जोड़ सकते हैं, मैं here जवाब दे दिया है की कोशिश कर सकते हैं।

संबंधित मुद्दे