Toni
Toni

Reputation: 173

Swagger/Swashbuckle Authorize. Scopes checked by default

Is it possible to set an auth scope checkbox checked by default on Swashbuckle UI on a asp.net Core 2.0 Web API??

I use the "openid" scope and I'd like to have it checked every time.

Thank you.

enter image description here

Upvotes: 13

Views: 2663

Answers (2)

Edyn
Edyn

Reputation: 2497

This is now possible. In the options for SwaggerUI use

options.OAuthScopes("<myapi>");

Upvotes: 6

Tom Makin
Tom Makin

Reputation: 3303

Here is hacktastic workaround for modern browsers which can be injected into index.html.

function checkScopes(container) {
  const inputNodes = container.querySelectorAll(".scopes input");
  for (const checkbox of inputNodes) {
    checkbox.click();
    console.log('Scope checkbox modified: ' + checkbox.id);
  }
}

function watchDOM() {
  // target element that we will observe
  const target = document.body;

  // subscriber function
  function subscriber(mutations) {
    mutations.forEach((mutation) => {
      if (mutation.target.className == 'auth-wrapper') {
        checkScopes(mutation.target);
      }
    });
  }

  // instantiating observer
  const observer = new MutationObserver(subscriber);

  // observing target
  observer.observe(target, { childList: true, subtree: true });
};

document.addEventListener('DOMContentLoaded', watchDOM);

Upvotes: 3

Related Questions