asp.net-core-webapi

Read JSON post data in ASP.Net Core MVC

纵然是瞬间 提交于 2019-11-30 18:11:17
I've tried to find a solution for this, but all the ones coming up are for previous versions of ASP.Net. I'm working with the JWT authentication middleware and have the following method: private async Task GenerateToken(HttpContext context) { var username = context.Request.Form["username"]; var password = context.Request.Form["password"]; //Remainder of login code } This gets the sent data as if it was form data, but my Angular 2 front end is sending the data as JSON. login(username: string, password: string): Observable<boolean> { let headers = new Headers({ 'Content-Type': 'application/json'

How to set base path property in swagger for .Net Core Web API

纵饮孤独 提交于 2019-11-30 15:11:22
问题 i've built a Web API in ASP.Net Core (version 1.1.2) and i use the Swashbuckle.AspNetCore for generating the swagger definition. below is a part of the automatically generated swagger definition. i would like to change the paths so it does not include /api/ApiName but it would be included in the basePath which is now / { "swagger": "2.0", "info": { "version": "v1", "title": "ApiName.V1" }, "host": "ApiUrl", "basePath": "/api/ApiName", "paths": { "/": { "get": { "tags": [ "ApiName" ], ........

The application completed without reading the entire request body, .net core 2.1.1

早过忘川 提交于 2019-11-30 13:01:27
问题 I have created a user register controller to register users with repository design pattern. My controller looks like this. [Route("api/[controller]")] public class AuthController : Controller { private readonly IAuthRepository _repo; public AuthController(IAuthRepository repo) { _repo = repo; } [AllowAnonymous] [HttpPost("register")] public async Task<IActionResult> Register([FromBody] UserForRegisterDto userForRegisterDto){ // validate request if(!ModelState.IsValid) return BadRequest

Mock HttpRequest in ASP.NET Core Controller

99封情书 提交于 2019-11-30 12:15:35
I'm building a Web API in ASP.NET Core, and I want to unit test the controllers. I inject an interface for data access, that I can easily mock. But the controller has to check the headers in the Request for a token, and that Request doesn't seem to exist when I simply instantiate the controller myself, and it is also get-only, so I can't even manually set it. I found lots of examples to mock an ApiController, but that isn't .NET core. Also many tutorials and examples of how to unit test .net core controllers, but none actually used the HttpRequest. I built an MCVE to demonstrate this:

Unit test controller model validation on AspNetCore

允我心安 提交于 2019-11-30 05:23:43
问题 In an ASPNET Core project I am trying to create some unit tests that would verify my data validation logic works fine. My controller is very simple: [HttpPost] [Route("Track")] public void Track([FromBody] DataItem item) { if (!ModelState.IsValid) throw new ArgumentException("Bad request"); _dataItemSaver.SaveData(item); } I am using a test base class that would set up the _myController object as the controller under test. public ControllerTestBase() { var builder = new ConfigurationBuilder()

.Net Core 2.0 Database First Approach Scaffold-DbContext of Mysql DB

我的梦境 提交于 2019-11-30 03:34:40
问题 I am developing WEB API in .Net Core 2.0 with MySQL DB. I am trying to scaffolding MySQL DB. I have follow This link (MySQL Official Site) but when I fire scaffolding command I getting error both I have mention below, please do let me know if I am doing anything wrong. Command For Scaffolding(firing in Package Manager Console) Scaffold-DbContext "server=localhost;port=3306;user=root;password=darshan7826;database=sakila" MySql.Data.EntityFrameworkCore -OutputDir sakila -f Error on executing

post multipart/form-data in c# HttpClient 4.5

有些话、适合烂在心里 提交于 2019-11-30 03:25:04
问题 Problem I am trying to post API to send data to API which calls my internal API service to send that data to other API i service. Entity contains property with files . this send only file to the other derive but the NameSender property not send with the file. Entity public class Email { public string NameSender{ get; set; } public List<IFormFile> Files { get; set; } } Api [Consumes("multipart/form-data")] [HttpPost] public IActionResult SendEmail([FromForm]Entity entity) { try { string

Access Raw Request Body

こ雲淡風輕ζ 提交于 2019-11-30 01:59:36
问题 I'm trying to access a request's raw input body/stream in ASP.net 5. In the past, I was able to reset the position of the input stream to 0 and read it into a memory stream but when I attempt to do this from the context the input stream is either null or throws an error (System.NotSupportedException => "Specified method is not supported."). In the first example below I can access the raw request in a controller if I declare the controller method's parameter object type as dynamic. For various

ASP.NET Core Web API Logging from a Static Class

这一生的挚爱 提交于 2019-11-29 17:06:46
问题 I'm logging just fine using dependency injection on my controllers, now I need to log something from a static class. How can I log from a static class? I can't use dependency injection because it's static and I can't just pass in an existing logger object to the static class because it would have the wrong name of class in the log file. In other words how can I get a logger from the loggerfactory inside my static class? I came across a similar question and they pointed to an article on a

ASP.NET Core JWT and Claims

这一生的挚爱 提交于 2019-11-29 16:29:11
问题 I have a question regarding JWT authentication in ASP.NET Core and Claims, because I don't know if I get everything correctly. When I create a JWT token in ASP.NET I add some Claims, some of which can be custom. What happens when the request with JWT token is sent from the client to API. How is User.Claims filled ? Does it use the claims that are read from JWT? I would like to create a custom Identity provider ( don't want to use this provided by ASP.NET), with my own tables for user data,