{"id":870,"date":"2021-07-24T11:30:00","date_gmt":"2021-07-23T22:30:00","guid":{"rendered":"https:\/\/blog.wiseowls.co.nz\/?p=870"},"modified":"2021-08-24T14:58:04","modified_gmt":"2021-08-24T01:58:04","slug":"setting-up-basic-auth-for-swashbuckle","status":"publish","type":"post","link":"https:\/\/blog.wiseowls.co.nz\/index.php\/2021\/07\/24\/setting-up-basic-auth-for-swashbuckle\/","title":{"rendered":"Setting up Basic Auth for Swashbuckle"},"content":{"rendered":"\n<p>Let us put the keywords up front: Swashbuckle Basic authentication setup that works for <a href=\"https:\/\/github.com\/domaindrivendev\/Swashbuckle.WebApi\">Swashbuckle.WebApi<\/a> installed on WebAPI projects (.NET 4.x). It&#8217;s also much less common to need Basic auth nowadays where proper mechanisms are easily available in all shapes and sizes. However, it may just become useful for internal tools or existing integrations. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why?<\/h2>\n\n\n\n<p>Most tutorials online would cover the next gen library aimed specifically at ASP.Net Core, which is great (we&#8217;re all in for tech advancement), but sometimes legacy needs a prop up. If you are looking to add Basic Auth to ASP.NET Core project &#8211; look up <code>AddSecurityDefinition<\/code> and go from there. If legacy is indeed the in trouble &#8211; read on.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Setting it up<\/h2>\n\n\n\n<p>The prescribed solution is two steps: <br>1. add authentication scheme on <code>SwaggerDocsConfig<\/code> class <br>2. attach authentication to endpoints as required with <code>IDocumentFilter<\/code> or <code>IOperationFilter<\/code><\/p>\n\n\n\n<p>Assuming the installer left us with <code>App_Start\/SwaggerConfig.cs<\/code> file, we&#8217;d arrive at:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"csharp\" class=\"language-csharp\">public class SwaggerConfig\r\n{\r\n\tpublic static void Register()\r\n\t{\r\n\t\tvar thisAssembly = typeof(SwaggerConfig).Assembly;\r\n\r\n\t\tGlobalConfiguration.Configuration\r\n\t\t\t.EnableSwagger(c =>\r\n\t\t\t\t{\r\n\t\t\t\t\t...\r\n\t\t\t\t\tc.BasicAuth(\"basic\").Description(\"Basic HTTP Authentication\");\r\n\t\t\t\t\tc.OperationFilter&lt;BasicAuthOpFilter>();\r\n\t\t\t\t\t...\r\n\t\t\t\t});\r\n\t}\r\n}<\/code><\/pre>\n\n\n\n<p>and the most basic <code>IOperationFilter<\/code> would look like so:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"csharp\" class=\"language-csharp\">public class BasicAuthOpFilter : IOperationFilter\r\n{\r\n\tpublic void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)\r\n\t{\r\n\t\toperation.security = operation.security ?? new List&lt;IDictionary&lt;string, IEnumerable&lt;string>>>();\r\n\t\toperation.security.Add(new Dictionary&lt;string, IEnumerable&lt;string>>()\r\n\t\t\t\t\t\t\t   {\r\n\t\t\t\t\t\t\t\t   { \"basic\", new List&lt;string>() }\r\n\t\t\t\t\t\t\t   });\r\n\t}\r\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Let us put the keywords up front: Swashbuckle Basic authentication setup that works for Swashbuckle.WebApi installed on WebAPI projects (.NET 4.x). It&#8217;s also much less common to need Basic auth nowadays where proper mechanisms are easily available in all shapes and sizes. However, it may just become useful for internal tools or existing integrations. Why? &hellip; <a href=\"https:\/\/blog.wiseowls.co.nz\/index.php\/2021\/07\/24\/setting-up-basic-auth-for-swashbuckle\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Setting up Basic Auth for Swashbuckle&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":566,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[11],"tags":[12,25,29],"class_list":["post-870","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dev","tag-c","tag-swagger","tag-webapi"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Setting up Basic Auth for Swashbuckle - Timur and associates<\/title>\n<meta name=\"description\" content=\"We&#039;ll have a look at Swashbuckle basic authentication setup. This method works with older library for .NET 4.x\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.wiseowls.co.nz\/index.php\/2021\/07\/24\/setting-up-basic-auth-for-swashbuckle\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Setting up Basic Auth for Swashbuckle - Timur and associates\" \/>\n<meta property=\"og:description\" content=\"We&#039;ll have a look at Swashbuckle basic authentication setup. This method works with older library for .NET 4.x\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.wiseowls.co.nz\/index.php\/2021\/07\/24\/setting-up-basic-auth-for-swashbuckle\/\" \/>\n<meta property=\"og:site_name\" content=\"Timur and associates\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-23T22:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-08-24T01:58:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/blog.wiseowls.co.nz\/wp-content\/uploads\/2020\/06\/padlock-code.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"312\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"timur\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@TimurKh\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2021\\\/07\\\/24\\\/setting-up-basic-auth-for-swashbuckle\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2021\\\/07\\\/24\\\/setting-up-basic-auth-for-swashbuckle\\\/\"},\"author\":{\"name\":\"timur\",\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/#\\\/schema\\\/person\\\/34d0ed30d573b5bc317ea990bd2e0c59\"},\"headline\":\"Setting up Basic Auth for Swashbuckle\",\"datePublished\":\"2021-07-23T22:30:00+00:00\",\"dateModified\":\"2021-08-24T01:58:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2021\\\/07\\\/24\\\/setting-up-basic-auth-for-swashbuckle\\\/\"},\"wordCount\":163,\"image\":{\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2021\\\/07\\\/24\\\/setting-up-basic-auth-for-swashbuckle\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/wp-content\\\/uploads\\\/2020\\\/06\\\/padlock-code.jpg\",\"keywords\":[\"c#\",\"swagger\",\"webapi\"],\"articleSection\":[\"Development\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2021\\\/07\\\/24\\\/setting-up-basic-auth-for-swashbuckle\\\/\",\"url\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2021\\\/07\\\/24\\\/setting-up-basic-auth-for-swashbuckle\\\/\",\"name\":\"Setting up Basic Auth for Swashbuckle - Timur and associates\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2021\\\/07\\\/24\\\/setting-up-basic-auth-for-swashbuckle\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2021\\\/07\\\/24\\\/setting-up-basic-auth-for-swashbuckle\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/wp-content\\\/uploads\\\/2020\\\/06\\\/padlock-code.jpg\",\"datePublished\":\"2021-07-23T22:30:00+00:00\",\"dateModified\":\"2021-08-24T01:58:04+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/#\\\/schema\\\/person\\\/34d0ed30d573b5bc317ea990bd2e0c59\"},\"description\":\"We'll have a look at Swashbuckle basic authentication setup. This method works with older library for .NET 4.x\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2021\\\/07\\\/24\\\/setting-up-basic-auth-for-swashbuckle\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2021\\\/07\\\/24\\\/setting-up-basic-auth-for-swashbuckle\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2021\\\/07\\\/24\\\/setting-up-basic-auth-for-swashbuckle\\\/#primaryimage\",\"url\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/wp-content\\\/uploads\\\/2020\\\/06\\\/padlock-code.jpg\",\"contentUrl\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/wp-content\\\/uploads\\\/2020\\\/06\\\/padlock-code.jpg\",\"width\":1280,\"height\":312},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2021\\\/07\\\/24\\\/setting-up-basic-auth-for-swashbuckle\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Setting up Basic Auth for Swashbuckle\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/#website\",\"url\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/\",\"name\":\"Timur and associates\",\"description\":\"Notes of an IT contractor\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/#\\\/schema\\\/person\\\/34d0ed30d573b5bc317ea990bd2e0c59\",\"name\":\"timur\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/23d55e17d4f0990ee4d12bc6e5dcfb58a292934efd62a185756876379e780b16?s=96&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/23d55e17d4f0990ee4d12bc6e5dcfb58a292934efd62a185756876379e780b16?s=96&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/23d55e17d4f0990ee4d12bc6e5dcfb58a292934efd62a185756876379e780b16?s=96&r=pg\",\"caption\":\"timur\"},\"sameAs\":[\"https:\\\/\\\/x.com\\\/TimurKh\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Setting up Basic Auth for Swashbuckle - Timur and associates","description":"We'll have a look at Swashbuckle basic authentication setup. This method works with older library for .NET 4.x","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.wiseowls.co.nz\/index.php\/2021\/07\/24\/setting-up-basic-auth-for-swashbuckle\/","og_locale":"en_US","og_type":"article","og_title":"Setting up Basic Auth for Swashbuckle - Timur and associates","og_description":"We'll have a look at Swashbuckle basic authentication setup. This method works with older library for .NET 4.x","og_url":"https:\/\/blog.wiseowls.co.nz\/index.php\/2021\/07\/24\/setting-up-basic-auth-for-swashbuckle\/","og_site_name":"Timur and associates","article_published_time":"2021-07-23T22:30:00+00:00","article_modified_time":"2021-08-24T01:58:04+00:00","og_image":[{"width":1280,"height":312,"url":"https:\/\/blog.wiseowls.co.nz\/wp-content\/uploads\/2020\/06\/padlock-code.jpg","type":"image\/jpeg"}],"author":"timur","twitter_card":"summary_large_image","twitter_creator":"@TimurKh","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2021\/07\/24\/setting-up-basic-auth-for-swashbuckle\/#article","isPartOf":{"@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2021\/07\/24\/setting-up-basic-auth-for-swashbuckle\/"},"author":{"name":"timur","@id":"https:\/\/blog.wiseowls.co.nz\/#\/schema\/person\/34d0ed30d573b5bc317ea990bd2e0c59"},"headline":"Setting up Basic Auth for Swashbuckle","datePublished":"2021-07-23T22:30:00+00:00","dateModified":"2021-08-24T01:58:04+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2021\/07\/24\/setting-up-basic-auth-for-swashbuckle\/"},"wordCount":163,"image":{"@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2021\/07\/24\/setting-up-basic-auth-for-swashbuckle\/#primaryimage"},"thumbnailUrl":"https:\/\/blog.wiseowls.co.nz\/wp-content\/uploads\/2020\/06\/padlock-code.jpg","keywords":["c#","swagger","webapi"],"articleSection":["Development"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2021\/07\/24\/setting-up-basic-auth-for-swashbuckle\/","url":"https:\/\/blog.wiseowls.co.nz\/index.php\/2021\/07\/24\/setting-up-basic-auth-for-swashbuckle\/","name":"Setting up Basic Auth for Swashbuckle - Timur and associates","isPartOf":{"@id":"https:\/\/blog.wiseowls.co.nz\/#website"},"primaryImageOfPage":{"@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2021\/07\/24\/setting-up-basic-auth-for-swashbuckle\/#primaryimage"},"image":{"@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2021\/07\/24\/setting-up-basic-auth-for-swashbuckle\/#primaryimage"},"thumbnailUrl":"https:\/\/blog.wiseowls.co.nz\/wp-content\/uploads\/2020\/06\/padlock-code.jpg","datePublished":"2021-07-23T22:30:00+00:00","dateModified":"2021-08-24T01:58:04+00:00","author":{"@id":"https:\/\/blog.wiseowls.co.nz\/#\/schema\/person\/34d0ed30d573b5bc317ea990bd2e0c59"},"description":"We'll have a look at Swashbuckle basic authentication setup. This method works with older library for .NET 4.x","breadcrumb":{"@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2021\/07\/24\/setting-up-basic-auth-for-swashbuckle\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.wiseowls.co.nz\/index.php\/2021\/07\/24\/setting-up-basic-auth-for-swashbuckle\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2021\/07\/24\/setting-up-basic-auth-for-swashbuckle\/#primaryimage","url":"https:\/\/blog.wiseowls.co.nz\/wp-content\/uploads\/2020\/06\/padlock-code.jpg","contentUrl":"https:\/\/blog.wiseowls.co.nz\/wp-content\/uploads\/2020\/06\/padlock-code.jpg","width":1280,"height":312},{"@type":"BreadcrumbList","@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2021\/07\/24\/setting-up-basic-auth-for-swashbuckle\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.wiseowls.co.nz\/"},{"@type":"ListItem","position":2,"name":"Setting up Basic Auth for Swashbuckle"}]},{"@type":"WebSite","@id":"https:\/\/blog.wiseowls.co.nz\/#website","url":"https:\/\/blog.wiseowls.co.nz\/","name":"Timur and associates","description":"Notes of an IT contractor","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.wiseowls.co.nz\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.wiseowls.co.nz\/#\/schema\/person\/34d0ed30d573b5bc317ea990bd2e0c59","name":"timur","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/23d55e17d4f0990ee4d12bc6e5dcfb58a292934efd62a185756876379e780b16?s=96&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/23d55e17d4f0990ee4d12bc6e5dcfb58a292934efd62a185756876379e780b16?s=96&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/23d55e17d4f0990ee4d12bc6e5dcfb58a292934efd62a185756876379e780b16?s=96&r=pg","caption":"timur"},"sameAs":["https:\/\/x.com\/TimurKh"]}]}},"_links":{"self":[{"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/posts\/870","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/comments?post=870"}],"version-history":[{"count":3,"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/posts\/870\/revisions"}],"predecessor-version":[{"id":873,"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/posts\/870\/revisions\/873"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/media\/566"}],"wp:attachment":[{"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/media?parent=870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/categories?post=870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/tags?post=870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}