Uso de api para leer y ejecutar sentencias SQL
This commit is contained in:
6
ApiDatosGuadex/ApiDatosGuadex.http
Normal file
6
ApiDatosGuadex/ApiDatosGuadex.http
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
@ApiExcelGuadex_HostAddress = http://localhost:5188
|
||||||
|
|
||||||
|
GET {{ApiExcelGuadex_HostAddress}}/weatherforecast/
|
||||||
|
Accept: application/json
|
||||||
|
|
||||||
|
###
|
||||||
28
ApiDatosGuadex/ApiDatosSanchoToro.csproj
Normal file
28
ApiDatosGuadex/ApiDatosSanchoToro.csproj
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
|
<PropertyGroup Label="Globals">
|
||||||
|
<SccProjectName>SAK</SccProjectName>
|
||||||
|
<SccProvider>SAK</SccProvider>
|
||||||
|
<SccAuxPath>SAK</SccAuxPath>
|
||||||
|
<SccLocalPath>SAK</SccLocalPath>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="EPPlus" Version="7.5.2" />
|
||||||
|
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="17.14.28" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.14" />
|
||||||
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
|
||||||
|
<PackageReference Include="tsUtilidades" Version="1.1.5" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\..\SanchoToro\bdGrupoSanchoToro\bdGrupoSanchoToro.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
88
ApiDatosGuadex/Controllers/HomeController.cs
Normal file
88
ApiDatosGuadex/Controllers/HomeController.cs
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
using ApiDatosGuadex.Models;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using System.Data.Common;
|
||||||
|
using bdGrupoSanchoToro.dbcontext;
|
||||||
|
using bdGrupoSanchoToro;
|
||||||
|
|
||||||
|
namespace ApiDatosGuadex.Controllers
|
||||||
|
{
|
||||||
|
[ApiController]
|
||||||
|
[Route("api/[controller]")]
|
||||||
|
public class HomeController : Controller
|
||||||
|
{
|
||||||
|
//metodo
|
||||||
|
[HttpGet("lee")]
|
||||||
|
public IActionResult LeeSQL(string bd, string sqlh)
|
||||||
|
{
|
||||||
|
// bdGestionGuadex.Utilidades.AñadeLog(tsUtilidades.Enumeraciones.TipoLog.Otros, "Inicio Log",null);
|
||||||
|
Response.ContentType = "text/plain";
|
||||||
|
try
|
||||||
|
{
|
||||||
|
|
||||||
|
DbConnection? db = null;
|
||||||
|
|
||||||
|
switch (bd.ToLower())
|
||||||
|
{
|
||||||
|
case "sanchotoro":
|
||||||
|
{
|
||||||
|
db = tscGrupoSanchoToro.NuevoContexto().Database.GetDbConnection();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
//case "guadex":
|
||||||
|
// {
|
||||||
|
// db = tscGrupoSanchoToro.NuevoContexto().Database.GetDbConnection();
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
|
|
||||||
|
}
|
||||||
|
// string res = tsUtilidades.bbdd.LeeMysql(db, sqlh);
|
||||||
|
string res = tsUtilidades.bbdd.LeeMysql(db, sqlh);
|
||||||
|
|
||||||
|
|
||||||
|
// termiancion en CRLF
|
||||||
|
res = res.Replace("\n", "\r\n");
|
||||||
|
|
||||||
|
return Content(res, "text/plain", System.Text.Encoding.UTF8);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
bdGrupoSanchoToro.db.Utilidades.AñadeLog(tsUtilidades.Enumeraciones.TipoLog.Fallo, "Error en LeeSQL", ex.Message, ex);
|
||||||
|
return Content($"Error: {ex.Message}\r\n", "text/plain", System.Text.Encoding.UTF8);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
public IActionResult EjeSQL(string bd, string sqlh)
|
||||||
|
{
|
||||||
|
Response.ContentType = "text/plain";
|
||||||
|
try
|
||||||
|
{
|
||||||
|
DbConnection? db = null;
|
||||||
|
switch (bd.ToLower())
|
||||||
|
{
|
||||||
|
case "sanchotoro":
|
||||||
|
{
|
||||||
|
db = tscGrupoSanchoToro.NuevoContexto().Database.GetDbConnection();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
//case "guadex":
|
||||||
|
// {
|
||||||
|
// db = bdGuadex.tscGuadex.NuevoContexto().Database.GetDbConnection();
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
string res = tsUtilidades.bbdd.EjeMySqlHex(db, sqlh);
|
||||||
|
|
||||||
|
// terminacion en CRLF
|
||||||
|
res = res.Replace("\n", "\r\n");
|
||||||
|
|
||||||
|
return Content(res, "text/plain", System.Text.Encoding.UTF8);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
bdGrupoSanchoToro.db.Utilidades.AñadeLog(tsUtilidades.Enumeraciones.TipoLog.Fallo, "Error en EjeSQL", ex.Message, ex);
|
||||||
|
return Content($"Error: {ex.Message}\r\n", "text/plain", System.Text.Encoding.UTF8);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
39
ApiDatosGuadex/Filtros/FiltroAutenticacionBasica.cs
Normal file
39
ApiDatosGuadex/Filtros/FiltroAutenticacionBasica.cs
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc.Filters;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
|
||||||
|
namespace ApiDatosGuadex.Filtros
|
||||||
|
{
|
||||||
|
public class FiltroAutenticacionBasica : Attribute, IAuthorizationFilter
|
||||||
|
{
|
||||||
|
private readonly string _usuarioPermitido;
|
||||||
|
private readonly string _contrasenaPermitida;
|
||||||
|
|
||||||
|
public FiltroAutenticacionBasica(IConfiguration configuration)
|
||||||
|
{
|
||||||
|
var authSettings = configuration.GetSection("Authentication");
|
||||||
|
_usuarioPermitido = authSettings["Username"];
|
||||||
|
_contrasenaPermitida = authSettings["Password"];
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnAuthorization(AuthorizationFilterContext contexto)
|
||||||
|
{
|
||||||
|
var encabezadoAutorizacion = contexto.HttpContext.Request.Headers["Authorization"].ToString();
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(encabezadoAutorizacion) || !encabezadoAutorizacion.StartsWith("Basic "))
|
||||||
|
{
|
||||||
|
contexto.Result = new UnauthorizedResult();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var credencialesCodificadas = encabezadoAutorizacion.Substring("Basic ".Length).Trim();
|
||||||
|
var credencialesDecodificadas = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(credencialesCodificadas));
|
||||||
|
|
||||||
|
var partes = credencialesDecodificadas.Split(':');
|
||||||
|
if (partes.Length != 2 || partes[0] != _usuarioPermitido || partes[1] != _contrasenaPermitida)
|
||||||
|
{
|
||||||
|
contexto.Result = new UnauthorizedResult();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
7
ApiDatosGuadex/Models/MensajeViewModel.cs
Normal file
7
ApiDatosGuadex/Models/MensajeViewModel.cs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
namespace ApiDatosGuadex.Models
|
||||||
|
{
|
||||||
|
public class MensajeViewModel
|
||||||
|
{
|
||||||
|
public string Mensaje { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
38
ApiDatosGuadex/Program.cs
Normal file
38
ApiDatosGuadex/Program.cs
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
using ApiDatosGuadex.Filtros;
|
||||||
|
|
||||||
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
|
// Agregar servicios al contenedor
|
||||||
|
builder.Services.AddControllersWithViews();
|
||||||
|
|
||||||
|
// Registrar el filtro de autenticaci<63>n como servicio
|
||||||
|
builder.Services.AddScoped<FiltroAutenticacionBasica>();
|
||||||
|
|
||||||
|
// Agregar Swagger
|
||||||
|
builder.Services.AddEndpointsApiExplorer();
|
||||||
|
builder.Services.AddSwaggerGen();
|
||||||
|
|
||||||
|
var app = builder.Build();
|
||||||
|
|
||||||
|
// Configuraci<63>n del pipeline de solicitudes HTTP
|
||||||
|
if (app.Environment.IsDevelopment())
|
||||||
|
{
|
||||||
|
app.UseSwagger();
|
||||||
|
app.UseSwaggerUI();
|
||||||
|
}
|
||||||
|
|
||||||
|
app.UseHttpsRedirection();
|
||||||
|
|
||||||
|
app.UseStaticFiles();
|
||||||
|
|
||||||
|
|
||||||
|
app.UseAuthorization();
|
||||||
|
|
||||||
|
app.MapControllers();
|
||||||
|
|
||||||
|
app.MapControllerRoute(
|
||||||
|
name: "default",
|
||||||
|
pattern: "{controller=Home}/{action=Index}/{id?}");
|
||||||
|
|
||||||
|
|
||||||
|
app.Run();
|
||||||
41
ApiDatosGuadex/Properties/launchSettings.json
Normal file
41
ApiDatosGuadex/Properties/launchSettings.json
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
{
|
||||||
|
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||||
|
"iisSettings": {
|
||||||
|
"windowsAuthentication": false,
|
||||||
|
"anonymousAuthentication": true,
|
||||||
|
"iisExpress": {
|
||||||
|
"applicationUrl": "http://localhost:12379",
|
||||||
|
"sslPort": 44309
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"profiles": {
|
||||||
|
"http": {
|
||||||
|
"commandName": "Project",
|
||||||
|
"dotnetRunMessages": true,
|
||||||
|
"launchBrowser": true,
|
||||||
|
"launchUrl": "swagger",
|
||||||
|
"applicationUrl": "http://localhost:5188",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"https": {
|
||||||
|
"commandName": "Project",
|
||||||
|
"dotnetRunMessages": true,
|
||||||
|
"launchBrowser": true,
|
||||||
|
"launchUrl": "swagger",
|
||||||
|
"applicationUrl": "https://localhost:7029;http://localhost:5188",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"IIS Express": {
|
||||||
|
"commandName": "IISExpress",
|
||||||
|
"launchBrowser": true,
|
||||||
|
"launchUrl": "swagger",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
3
ApiDatosGuadex/Views/Home/EjeSQL.cshtml
Normal file
3
ApiDatosGuadex/Views/Home/EjeSQL.cshtml
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
@ViewData["Mensaje"]
|
||||||
|
|
||||||
|
|
||||||
4
ApiDatosGuadex/Views/Home/LeeSQL.cshtml
Normal file
4
ApiDatosGuadex/Views/Home/LeeSQL.cshtml
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
@ViewData["Mensaje"]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
8
ApiDatosGuadex/appsettings.Development.json
Normal file
8
ApiDatosGuadex/appsettings.Development.json
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
13
ApiDatosGuadex/appsettings.json
Normal file
13
ApiDatosGuadex/appsettings.json
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"AllowedHosts": "*",
|
||||||
|
"Authentication": {
|
||||||
|
"Username": "guadex",
|
||||||
|
"Password": "tsguadex..-"
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user