asp.net-core-webapi

Empty authorization header on requests for Swashbuckle.AspNetCore

一笑奈何 提交于 2019-12-31 06:29:33
问题 Currently having an issue with authorization headers in swashbuckle for .net core The first line of code on every endpoint is: string auth = Request.Headers["Authorization"]; When using postman, everything works smoothly, but when making a request from localhost/swagger, the header is empty when a breakpoint is inserted, the header is a null value. the body of the request is in tact and everything works properly when the authorization is removed from the endpoint In my services.AddSwaggerGen

Empty authorization header on requests for Swashbuckle.AspNetCore

感情迁移 提交于 2019-12-31 06:29:04
问题 Currently having an issue with authorization headers in swashbuckle for .net core The first line of code on every endpoint is: string auth = Request.Headers["Authorization"]; When using postman, everything works smoothly, but when making a request from localhost/swagger, the header is empty when a breakpoint is inserted, the header is a null value. the body of the request is in tact and everything works properly when the authorization is removed from the endpoint In my services.AddSwaggerGen

ASP.Net Identity built in functions with custom tables in ASP.Net Core

余生颓废 提交于 2019-12-31 02:39:13
问题 I am using ASP.Net Core Web Api 2 on .Net 2.1 Framework I have custom AppUsers and AppRoles tables, linked with bridge table AppUserRoles My main problem is that I want to use [Authorize(Roles = "UserRole")] As User.Identity is working fine and I am getting user Id from User.Identity.Name I thought there was some way to set roles and check them before controller request, or to use User.IsInRole("UserRole") for checking inside controller. Is it possible to rebuild or overload .IsInRole(

What's the point of hosting.json since appsettings.json is sufficient

前提是你 提交于 2019-12-30 07:07:28
问题 In .NET Core 2 Web API app, I could override configuration urls using appsettings.json , but in the official docs they introduced extra file "hosting.json", Why? What's the point of adding complexity? Below code is fully working using appsettings.json : public class Program { public static void Main(string[] args) { BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) { var config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) //see Side

Return jpeg image from Asp.Net Core WebAPI

笑着哭i 提交于 2019-12-29 04:29:15
问题 Using asp.net core web api , I want to have my controller action method to return an jpeg image stream . In my current implementation, browser displays only a json string . My expectation is to see the image in the browser. While debugging using chrome developer tools I found that the content type is still Content-Type:application/json; charset=utf-8 returned in the response header, even though in my code I manually set the content type to "image/jpeg". Looking for a solution My Web API is as

How to add custom header to ASP.NET Core Web API response

丶灬走出姿态 提交于 2019-12-29 03:32:28
问题 I am porting my API from Web API 2 to ASP.NET Core Web API. I used to be able to add a custom header in the following manner: HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); response.Headers.Add("X-Total-Count", count.ToString()); return ResponseMessage(response); How does one add a custom header in ASP.NET Core Web API? 回答1: You can just hi-jack the HttpContext from the incoming Http Request and add your own custom headers to the Response object before calling

WebApi giving 404 whilst debugging; works when published

大兔子大兔子 提交于 2019-12-29 00:53:13
问题 I have a console application written in .NET Core 2.2.6 that is using Kestrel to host a simple WebApi. public class SettingsController : Controller { // // GET: /settings/ public string Index() { return $"Hello world! controller"; } } If I publish the code and run the executable, I can visit http://127.0.0.1:310/settings and see the expected "Hello world! controller". However, if I debug (or even open in Release mode) from inside Visual Studio 2019, the same URL throws a 404 exception. Some

How to implement a “pure” ASP.NET Core Web API by using AddMvcCore()

谁说我不能喝 提交于 2019-12-28 04:50:09
问题 I've seen a lot of ASP.NET Core Web API projects that use the default AddMvc() service without the realizing that using AddMvcCore() is a superior option due to the control over services. How exactly do you implement an ASP.NET Core Web API by using AddMvcCore() and why is it better? 回答1: What is the difference between AddMvc() and AddMvcCore() ? The first thing key thing to understand is that AddMvc() is just a pre-loaded version of AddMvcCore() . You can see the exact implementation of the

Data models not showing up in HttpResponseMessage

ぃ、小莉子 提交于 2019-12-28 03:10:32
问题 I'm trying to make a simple API call from a .NET Core MVC application: using (var client = new HttpClient()) { client.BaseAddress = new Uri("http://localhost:49897"); var response = client.GetAsync("some-route").Result; var dataString = response.Content.ReadAsStringAsync().Result; // Unexpected data here. See below. [...] // deserialize dataString } client.GetAsync(route) successfully hits an API action method, which ultimately does this: public HttpResponseMessage Get([FromUri] BindingModel

How to use OData in ASP.NET Core 2.1?

て烟熏妆下的殇ゞ 提交于 2019-12-25 17:46:53
问题 I try to use OData in an ASP. Bellow my code: //============== Startup.cs public void ConfigureServices(IServiceCollection services) { services.AddDbContext<EntriesContext>( opt => opt.UseMySql("server=localhost;database=mydb;user=myusr;password=mypass", mysqlOptions =>{mysqlOptions.ServerVersion(new Version(5..), ServerType.MySql);})); services.AddOData(); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); ... } public void Configure(IApplicationBuilder app,