Designbest Rest API: differenze tra le versioni
Nessun oggetto della modifica |
|||
| Riga 83: | Riga 83: | ||
* <p><code>InjectStylesheet()</code> e <code>InjectJavascript()</code> servono a personalizzare la grafica.<br/>Perché questi funzionino è necessario attivare <code>app.UseStaticFiles();</code> e creare la cartella <code>wwwroot</code> che conterrà le cartelle <code>css</code> e <code>js</code></p> | * <p><code>InjectStylesheet()</code> e <code>InjectJavascript()</code> servono a personalizzare la grafica.<br/>Perché questi funzionino è necessario attivare <code>app.UseStaticFiles();</code> e creare la cartella <code>wwwroot</code> che conterrà le cartelle <code>css</code> e <code>js</code></p> | ||
* Nel file <code>Properties/launchSettings.json</code> modificare i campi ''launchUrl'' con la stringa <del>''"swagger"''</del> => ''"designbest"''. | * Nel file <code>Properties/launchSettings.json</code> modificare i campi ''launchUrl'' con la stringa <del>''"swagger"''</del> => ''"designbest"''. | ||
== Verb PUT e DELETE == | |||
A quanto pare esiste un modulo chiamato '''WebDAV''' su Windows Server che intercetta tutte le chiamate <code>PUT</code> e <code>DELETE</code> e non permette che arrivino alla RestAPI. | |||
Il modulo deve essere disattivato dal sito tramite il <code>web.config</code>. | |||
Creare nella solution un file <code>web.release.config</code> col seguente codice: | |||
<syntaxhighlight lang="xml"> | |||
<?xml version="1.0" encoding="utf-8"?> | |||
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> | |||
<location> | |||
<system.webServer> | |||
<modules xdt:Transform="Insert"> | |||
<remove name="WebDAVModule" /> | |||
</modules> | |||
<handlers> | |||
<remove name="WebDAV" xdt:Transform="Insert" /> | |||
</handlers> | |||
</system.webServer> | |||
</location> | |||
</configuration> | |||
</syntaxhighlight> | |||
Andare su ''Properties'' e nella ''Build Action'' segnare ''None''. | |||
Versione delle 07:57, 22 mag 2025
Access Token
Token valido 100 anni
Per servizi che hanno bisogno di un token fisso e non possono fare la richiesta ogni volta (es. Zapier),
ecco un token che dura 100 anni:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c3IiOiJkZXNpZ25iZXN0cmVzdCIsImV4cCI6NDg4Nzk0MjU5OSwiaXNzIjoid2VibW9iaWxpIiwiYXVkIjoiZGVzaWduYmVzdCJ9.Kqvuli0CAdaX5-Qn-Fxx9i01IWTjjqktwrXZOGdfZ5Q
Test
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c3IiOiJkZXNpZ25iZXN0cmVzdCIsImV4cCI6MTczMjI2OTEwMCwiaXNzIjoid2VibW9iaWxpIiwiYXVkIjoiZGVzaWduYmVzdCJ9.Vbd9m67qhx_T0DgsmSnH5Zn8Vz6j7Dxpu1vsEFyFSSk
Configurazione di Swagger
Nel file Program.cs configurare così
const string adminDocName = "v1";
const string clientsDocName = "clients";
builder.Services.AddSwaggerGen(
opt => {
// SwaggerDoc Admin
opt.SwaggerDoc(adminDocName, new() {
Title = "Designbest REST API",
Description = "La Designbest REST API fornisce un accesso completo e flessibile al portale leader mondiale dell'arredamento.",
Version = "v1",
Contact = new() { Email = "info@designbest.com", Name = "Webmobili s.r.l." }
});
// SwaggerDoc Client
opt.SwaggerDoc("clients", new() {
Title = "Designbest REST API for clients",
Description = "La Designbest REST API fornisce un'interfaccia di accesso dati completa e flessibile sul portale leader mondiale dell'arredamento.",
Version = "v1",
Contact = new() { Email = "info@designbest.com", Name = "Webmobili s.r.l." }
});
var tokenService = new TokenService(builder.Configuration, builder.Environment);
const string securitySchemeName = JwtBearerDefaults.AuthenticationScheme;
opt.AddSecurityDefinition(securitySchemeName, new() {
In = ParameterLocation.Header,
Description = "Please enter a valid token like " + tokenService.GetToken()?.AccessToken,
Name = "Authorization",
Type = SecuritySchemeType.Http,
BearerFormat = "JWT",
Scheme = "Bearer"
});
opt.AddSecurityRequirement(new() {
{
new() {
Reference = new() {
Type = ReferenceType.SecurityScheme,
Id = securitySchemeName
}
},
Array.Empty<string>()
}
});
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
opt.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFile));
}
);
e di sotto, per poter cambiare il nome dell'endpoint della documentazione in /designbest e personalizzare la grafica
app.UseSwagger(options => {
options.RouteTemplate = "designbest/{documentName}/swagger.json";
});
if (app.Environment.IsDevelopment() || app.Environment.IsStaging()) {
app.UseSwaggerUI(options => {
options.DocumentTitle = "Designbest REST API";
options.InjectStylesheet("/css/designbest-swagger.css");
options.InjectJavascript("/js/swagger-custom.js");
options.RoutePrefix = "designbest";
options.SwaggerEndpoint($"/designbest/{adminDocName}/swagger.json", "Designbest REST API"); // "v1" corrisponde al primo parametro di SwaggerDoc("v1", new() {
options.SwaggerEndpoint($"/designbest/{clientsDocName}/swagger.json", "Designbest REST API for clients"); // "clients" corrisponde al primo parametro di SwaggerDoc("client", new() {
});
}
// ...
app.MapStaticAssets();
RouteTemplate,RoutePrefixeSwaggerEndpoint()sono collegati e servono a cambiare il nome default per raggiungere la url della documentazioneInjectStylesheet()eInjectJavascript()servono a personalizzare la grafica.
Perché questi funzionino è necessario attivareapp.UseStaticFiles();e creare la cartellawwwrootche conterrà le cartellecssejs- Nel file
Properties/launchSettings.jsonmodificare i campi launchUrl con la stringa"swagger"=> "designbest".
Verb PUT e DELETE
A quanto pare esiste un modulo chiamato WebDAV su Windows Server che intercetta tutte le chiamate PUT e DELETE e non permette che arrivino alla RestAPI.
Il modulo deve essere disattivato dal sito tramite il web.config.
Creare nella solution un file web.release.config col seguente codice:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<location>
<system.webServer>
<modules xdt:Transform="Insert">
<remove name="WebDAVModule" />
</modules>
<handlers>
<remove name="WebDAV" xdt:Transform="Insert" />
</handlers>
</system.webServer>
</location>
</configuration>
Andare su Properties e nella Build Action segnare None.