2015-03-04 12 views
10

मैं पुस्तकालय स्वाशबकल का उपयोग कर रहा हूं। वर्तमान में इसके लिए कोई स्टैक ओवरफ्लो टैग नहीं है।मैं स्वाशबकल में हेडर डॉक्यूमेंटेशन कैसे जोड़ूं?

मैं काफी प्रलेखन यहाँ समझ में नहीं आता: https://github.com/domaindrivendev/Swashbuckle/blob/master/README.md

शीर्षक "बताते सुरक्षा/प्राधिकरण योजनाएं" खंड कोड का एक टुकड़ा का उल्लेख है

c.ApiKey("apiKey") 
       .Description("API Key Authentication") 
       .Name("apiKey") 
       .In("header"); 

हालांकि जब मैं शामिल इस कुछ नहीं होता। मैं इसे कुछ एपीआई तरीकों पर ही दिखाना चाहूंगा। यह उल्लेख

" दस्तावेज़ पर संपत्ति" सुरक्षा "एक इसी के साथ मिलकर करने की आवश्यकता है" लेकिन मैं यह समझ में नहीं आता है।

क्या कोई समझा सकता है?

+0

किया? –

+0

मेरी इच्छा है कि यह इतना आसान था :-) हां मैंने इसे – LivingOnACloud

उत्तर

4

मैं एक ही सवाल था और यह इस तरह का समाधान:

SwaggerConfig में:

var applyApiKeySecurity = new ApplyApiKeySecurity(
    key: "ServiceBusToken", 
    name: "Authorization", 
    description: "Service Bus Token, e.g. 'SharedAccessSignature sr=...&sig=...&se=...&skn=...'", 
    @in: "header" 
); 
applyApiKeySecurity.Apply(c); 

ApplyApiKeySecurity:

public class ApplyApiKeySecurity : IDocumentFilter, IOperationFilter 
{ 
    public ApplyApiKeySecurity(string key, string name, string description, string @in) 
    { 
     Key = key; 
     Name = name; 
     Description = description; 
     In = @in; 
    } 

    public string Description { get; private set; } 

    public string In { get; private set; } 

    public string Key { get; private set; } 

    public string Name { get; private set; } 

    public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, System.Web.Http.Description.IApiExplorer apiExplorer) 
    { 
     IList<IDictionary<string, IEnumerable<string>>> security = new List<IDictionary<string, IEnumerable<string>>>(); 
     security.Add(new Dictionary<string, IEnumerable<string>> { 
      {Key, new string[0]} 
     }); 

     swaggerDoc.security = security; 
    } 

    public void Apply(Operation operation, SchemaRegistry schemaRegistry, System.Web.Http.Description.ApiDescription apiDescription) 
    { 
     operation.parameters = operation.parameters ?? new List<Parameter>(); 
     operation.parameters.Add(new Parameter 
     { 
      name = Name, 
      description = Description, 
      @in = In, 
      required = true, 
      type = "string" 
     }); 
    } 

    public void Apply(Swashbuckle.Application.SwaggerDocsConfig c) 
    { 
     c.ApiKey(Key) 
      .Name(Name) 
      .Description(Description) 
      .In(In); 
     c.DocumentFilter(() => this); 
     c.OperationFilter(() => this); 
    } 
} 

फिर अकड़ फ़ाइल सुरक्षा definiton है:

"securityDefinitions":{ 
    "ServiceBusToken":{ 
    "type":"apiKey", 
    "description":"Service Bus Token, e.g. 'SharedAccessSignature sr=...&sig=...&se=...&skn=...'", 
    "name":"Authorization", 
    "in":"header" 
    } 
} 
"parameters":[ 
    { 
     "name":"Authorization", 
     "in":"header", 
     "description":"Service Bus Token, e.g. 'SharedAccessSignature sr=...&sig=...&se=...&skn=...'", 
     "required":true, 
     "type":"string" 
    } 
] 
2

Swashbuckle मेंटेनर ऐसा करने के लिए, क्योंकि वह निकाल देंगे एक कस्टम index.html प्रदान करने के लिए हमें सलाह देता है:

"security":[ 
    { 
    "ServiceBusToken":[] 
    } 
] 

और सभी कार्यों हैडर पैरामीटर सौंपा है:दस्तावेज़ स्तर पर सभी कार्यों के लिए आवेदन किया अगले प्रमुख संस्करण में इन विन्यास। यह issue देखें।

अपने खुद के "सूचकांक" फाइल

उपयोग CustomAsset विकल्प Swashbuckle निर्देश देने के लिए कोई अनुरोध "अनुक्रमणिका" के लिए किया जाता है डिफ़ॉल्ट के बजाय अपने संस्करण वापस जाने के लिए प्रदान करें। सभी कस्टम सामग्री के साथ, फ़ाइल को आपके प्रोजेक्ट में "एंबेडेड रिसोर्स" के रूप में शामिल किया जाना चाहिए, और उसके बाद संसाधन का "लॉजिकल नेम" नीचे दिखाए गए तरीके से पास किया गया है। चरण-दर-चरण निर्देशों के लिए Injecting Custom Content देखें।

संगतता के लिए, आपको this version से अपने कस्टम "index.html" का आधार बनाना चाहिए।

httpConfiguration 
    .EnableSwagger(c => c.SingleApiVersion("v1", "A title for your API")) 
    .EnableSwaggerUi(c => 
     { 
      c.CustomAsset("index", yourAssembly, "YourWebApiProject.SwaggerExtensions.index.html"); 
     }); 

index.html में आप कुछ इस तरह करने के लिए नीचे दी गई विधि को बदलना चाहते हैं जाएगा: आप इसे uncommenting कोशिश

function addApiKeyAuthorization(){ 
    var key = encodeURIComponent($('#input_apiKey')[0].value); 
    if(key && key.trim() != "") { 
     var apiKeyAuth = new SwaggerClient.ApiKeyAuthorization("sessionId", key, "header"); 
     window.swaggerUi.api.clientAuthorizations.add("sessionId", apiKeyAuth); 
     log("added key " + key); 
    } 
} 
+0

को असम्बद्ध करने का प्रयास किया था। यह सब कुछ – LivingOnACloud

+0

निश्चित रूप से कॉन्फ़िगर करने की तुलना में हैक की तरह लगता है। सुंदर नहीं। मैं आपके प्रश्न पर हमें दिखाए गए कॉन्फ़िगरेशन का उपयोग करना पसंद करूंगा लेकिन रखरखाव इसे हटा देगा ... :-(और यह भी बग के कारण काम नहीं करता है। –

+1

इंजेक्शन जावास्क्रिप्ट का उपयोग बेहतर समाधान होना चाहिए। इस समस्या को थ्रेड देखें: https://github.com/domaindrivendev/Swashbuckle/issues/222 मैं http शीर्षलेख में प्राधिकरण के लिए एक भालू टोकन जोड़ने के लिए इस दृष्टिकोण का उपयोग करता हूं। –

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