Skip to content

.netCore 3, entity framework, swagger, WebApi, identity, jwt token

Notifications You must be signed in to change notification settings

pdgramajo/ApiBase

Repository files navigation

EF core important command

WebApi .NetCore 3.0

descargar el sdk e instalar

  • https://dotnet.microsoft.com/download/dotnet-core/3.0

  • dotnet add package Microsoft.AspNetCore.Authentication.JwtBearer --version 3.0.0

  • dotnet add package Microsoft.AspNetCore.Identity.EntityFrameworkCore --version 3.0.0

  • dotnet add package Microsoft.EntityFrameworkCore.Design --version 3.0.0

  • dotnet add package Microsoft.AspNetCore.Cors --version 2.2.0

Troubleshoot certificate problems

  • This section provides help when the ASP.NET Core HTTPS development certificate has been installed and trusted, but you still have browser warnings that the certificate is not trusted.
  • All platforms - certificate not trusted
  • Run the following commands:
dotnet dev-certs https --clean
dotnet dev-certs https --trust

nuget

  • para remover un nuget dotnet remove package Microsoft.Extensions.DependencyInjection

cors

Install-Package Microsoft.AspNetCore.Cors

in the startup.cs in the configure method

app.UseCors(options => options.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());

Config

crear una carpeta Properties y crear un archivo launchSettings.json deveria de quedar asi: Properties/launchSettings.json

copiar en el archivo este contenido reemplazando el valor de SUPER_SECRET_KEY

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false, 
    "anonymousAuthentication": true, 
    "iisExpress": {
      "applicationUrl": "http://localhost:34716",
      "sslPort": 44347
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "index.html",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "SUPER_SECRET_KEY": "MY_SUPER_SECRET_KEY",
        "STRING_CONNECTION":"Server=PABLO-GRAMAJO\\SQLEXPRESSXXXX;Database=TesisBD;Trusted_Connection=True;"
      }
    },
    "api": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "index.html",
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "SUPER_SECRET_KEY": "MY_SUPER_SECRET_KEY",
        "STRING_CONNECTION":"Server=PABLO-GRAMAJO\\SQLEXPRESSXXXX;Database=TesisBD;Trusted_Connection=True;"
      }
    }
  }
}

Swagger Configuration

Get started with Swashbuckle and ASP.NET Core

el siguiente comando instala el paquete completo de swagger

dotnet add TodoApi.csproj package Swashbuckle.AspNetCore -v 5.0.0-rc4

  1. Add the Swagger generator to the services collection in the Startup.ConfigureServices method:
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<TodoContext>(opt =>
            opt.UseInMemoryDatabase("TodoList"));
        services.AddControllers();

        // Register the Swagger generator, defining 1 or more Swagger documents
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
        });
    }
  1. In the Startup.Configure method, enable the middleware for serving the generated JSON document and the Swagger UI:
    public void Configure(IApplicationBuilder app)
    {
        // Enable middleware to serve generated Swagger as a JSON endpoint.
        app.UseSwagger();

        // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
        // specifying the Swagger JSON endpoint.
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
            c.RoutePrefix = string.Empty;
        });

        app.UseRouting();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
  1. In the Startup class, add the following namespaces:
using System;
using System.Reflection;
using System.IO;
  1. The configuration action passed to the AddSwaggerGen method adds information such as the author, license, and description:
    // Register the Swagger generator, defining 1 or more Swagger documents
    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new OpenApiInfo
        {
            Version = "v1",
            Title = "ToDo API",
            Description = "A simple example ASP.NET Core Web API",
            TermsOfService = new Uri("https://example.com/terms"),
            Contact = new OpenApiContact
            {
                Name = "Shayne Boyer",
                Email = string.Empty,
                Url = new Uri("https://twitter.com/spboyer"),
            },
            License = new OpenApiLicense
            {
                Name = "Use under LICX",
                Url = new Uri("https://example.com/license"),
            }
        });
    });
  1. Adding comments in the endpoint:
    1. Manually add the highlighted lines to the .csproj file:
        <PropertyGroup>
            <GenerateDocumentationFile>true</GenerateDocumentationFile>
            <NoWarn>$(NoWarn);1591</NoWarn>
        </PropertyGroup>
    
    1. Configure Swagger to use the XML file that's generated with the preceding instructions. For Linux or non-Windows operating systems, file names and paths can be case-sensitive. For example, a TodoApi.XML file is valid on Windows but not CentOS
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<TodoContext>(opt =>
                opt.UseInMemoryDatabase("TodoList"));
            services.AddControllers();
    
            // Register the Swagger generator, defining 1 or more Swagger documents
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version = "v1",
                    Title = "ToDo API",
                    Description = "A simple example ASP.NET Core Web API",
                    TermsOfService = new Uri("https://example.com/terms"),
                    Contact = new OpenApiContact
                    {
                        Name = "Shayne Boyer",
                        Email = string.Empty,
                        Url = new Uri("https://twitter.com/spboyer"),
                    },
                    License = new OpenApiLicense
                    {
                        Name = "Use under LICX",
                        Url = new Uri("https://example.com/license"),
                    }
                });
    
                // Set the comments path for the Swagger JSON and UI.
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);
            });
        }
    
    1. Adding triple-slash comments to an action enhances the Swagger UI by adding the description to the section header. Add a element above the Delete action:
        /// <summary>
        /// Deletes a specific TodoItem.
        /// </summary>
        /// <param name="id"></param>        
        [HttpDelete("{id}")]
        public IActionResult Delete(long id)
        {
            var todo = _context.TodoItems.Find(id);
    
            if (todo == null)
            {
                return NotFound();
            }
    
            _context.TodoItems.Remove(todo);
            _context.SaveChanges();
    
            return NoContent();
        }
    
    1. Add a element to the Create action method documentation. It supplements information specified in the element and provides a more robust Swagger UI. The element content can consist of text, JSON, or XML.
        /// <summary>
        /// Creates a TodoItem.
        /// </summary>
        /// <remarks>
        /// Sample request:
        ///
        ///     POST /Todo
        ///     {
        ///        "id": 1,
        ///        "name": "Item1",
        ///        "isComplete": true
        ///     }
        ///
        /// </remarks>
        /// <param name="item"></param>
        /// <returns>A newly created TodoItem</returns>
        /// <response code="201">Returns the newly created item</response>
        /// <response code="400">If the item is null</response>            
        [HttpPost]
        [ProducesResponseType(201)]
        [ProducesResponseType(400)]
        public ActionResult<TodoItem> Create(TodoItem item)
        {
            _context.TodoItems.Add(item);
            _context.SaveChanges();
    
            return CreatedAtRoute("GetTodo", new { id = item.Id }, item);
        } 
    
  2. Add the [Produces("application/json")] attribute to the API controller. Its purpose is to declare that the controller's actions support a response content type of application/json:
    [Produces("application/json")]
    [Route("api/[controller]")]
    [ApiController]
    public class TodoController : ControllerBase
    {
        private readonly TodoContext _context;
  1. Describe response types

Developers consuming a web API are most concerned with what's returned—specifically response types and error codes (if not standard). The response types and error codes are denoted in the XML comments and data annotations.

The Create action returns an HTTP 201 status code on success. An HTTP 400 status code is returned when the posted request body is null. Without proper documentation in the Swagger UI, the consumer lacks knowledge of these expected outcomes. Fix that problem by adding the highlighted lines in the following example:

    /// <summary>
    /// Creates a TodoItem.
    /// </summary>
    /// <remarks>
    /// Sample request:
    ///
    ///     POST /Todo
    ///     {
    ///        "id": 1,
    ///        "name": "Item1",
    ///        "isComplete": true
    ///     }
    ///
    /// </remarks>
    /// <param name="item"></param>
    /// <returns>A newly created TodoItem</returns>
    /// <response code="201">Returns the newly created item</response>
    /// <response code="400">If the item is null</response>            
    [HttpPost]
    [ProducesResponseType(201)]
    [ProducesResponseType(400)]
    public ActionResult<TodoItem> Create(TodoItem item)
    {
        _context.TodoItems.Add(item);
        _context.SaveChanges();

        return CreatedAtRoute("GetTodo", new { id = item.Id }, item);
    }

para agregar seguridad me base en el siguiente link

https://stackoverflow.com/questions/56234504/migrating-to-swashbuckle-aspnetcore-version-5

        c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
        {
            Description =
                "JWT Authorization header using the Bearer scheme. \r\n\r\n Enter 'Bearer' [space] and then your token in the text input below.\r\n\r\nExample: \"Bearer 12345abcdef\"",
            Name = "Authorization",
            In = ParameterLocation.Header,
            Type = SecuritySchemeType.ApiKey,
            Scheme = "Bearer"
        });

        c.AddSecurityRequirement(new OpenApiSecurityRequirement()
        {
            {
                new OpenApiSecurityScheme
                {
                    Reference = new OpenApiReference
                    {
                        Type = ReferenceType.SecurityScheme,
                        Id = "Bearer"
                    },
                    Scheme = "oauth2",
                    Name = "Bearer",
                    In = ParameterLocation.Header,

                },
                new List<string>()
            }
        });

About

.netCore 3, entity framework, swagger, WebApi, identity, jwt token

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages