asp.net-core-webapi

Enable OPTIONS header for CORS on .NET Core Web API

拥有回忆 提交于 2019-11-27 19:50:49
I solved this problem after not finding the solution on Stackoverflow, so I am sharing my problem here and the solution in an answer. After enabling a cross domain policy in my .NET Core Web Api application with AddCors, it still does not work from browsers. This is because browsers, including Chrome and Firefox, will first send an OPTIONS request and my application just responds with 204 No Content. Niels Brinch Add a middleware class to your project to handle the OPTIONS verb. using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft

Creating a proxy to another web api with Asp.net core

穿精又带淫゛_ 提交于 2019-11-27 17:51:20
I'm developing an ASP.Net Core web application where I need to create a kind of "authentication proxy" to another (external) web service. What I mean by authentication proxy is that I will receive requests through a specific path of my web app and will have to check the headers of those requests for an authentication token that I'll have issued earlier, and then redirect all the requests with the same request string / content to an external web API which my app will authenticate with through HTTP Basic auth. Here's the whole process in pseudo-code Client requests a token by making a POST to a

Mock HttpRequest in ASP.NET Core Controller

℡╲_俬逩灬. 提交于 2019-11-27 17:40:51
问题 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

How to return a specific status code and no contents from Controller?

末鹿安然 提交于 2019-11-27 17:11:39
I want the example controller below to return a status code 418 with no contents. Setting the status code is easy enough but then it seems like there is something that needs to be done to signal the end of the request. In MVC prior to ASP.NET Core or in WebForms that might be a call to Response.End() but how does it work in ASP.NET Core where Response.End does not exist? public class ExampleController : Controller { [HttpGet][Route("/example/main")] public IActionResult Main() { this.HttpContext.Response.StatusCode = 418; // I'm a teapot // How to end the request? // I don't actually want to

Loading and registering API Controllers From Class Library in ASP.NET core

眉间皱痕 提交于 2019-11-27 16:15:07
问题 I am using ASP.NET Core 1.0.1. I have the following A class library that uses "Microsoft.AspNetCore.Mvc": "1.0.1" in order to develop my controllers: using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; namespace CoreAPIsLibrary.Controllers { [Route("api/[controller]")] public class ValuesContoller : Controller { public string Get() { return "value"; } // GET api/values/5 [HttpGet("{id}")] public string Get(int id) {

Registering a new DelegatingHandler in ASP.NET Core Web API

删除回忆录丶 提交于 2019-11-27 15:40:21
问题 I want to create a new Handler that extends DelegatingHandler to enable me to do stuff before getting as far as the controller. I have read in various places that I need need to inherit from DelegatingHandler then overrride SendAsync() like this: public class ApiKeyHandler : DelegatingHandler { protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { // do custom stuff here return base.SendAsync(request, cancellationToken); } }

Still logged in MVC site, but can't call web API

青春壹個敷衍的年華 提交于 2019-11-27 14:54:42
I have an ASP.NET MVC site, IdentityServer4 host and a web API. When I log in the MVC site, using external provider (Facebook), I'm logged in fine. From the MVC site I can also consume the web API correctly. However, the next day, I'm still logged in into the MVC site, but when I then try to access the web API, I get a 'not authorized exception'. So although I'm still logged in in the MVC site, I'm not authenticated anymore to call a web API from within the MVC site. I'm wondering how to handle this situation, and how IdentityServer4 should be configured. Why am I still logged in the MVC site

file upload and download in angular 4 typescript

倾然丶 夕夏残阳落幕 提交于 2019-11-27 10:57:46
问题 How can I download ( .exe file which is in root path) and Upload a file from Angular 4?. I am new to Angular4 and typescript and .NET Core Web API. I have googled for this but could not find the solution. Here are some similar questions that I found: Uploading file to controller using typescript Returning binary file from controller in ASP.NET Web API 回答1: I'd like to add an Angular 4.3/5/6/7/8 update for this especially vis-a-vis the simplified HttpClient. The absence of the 'Content-Type'

ASP.NET Core JWT mapping role claims to ClaimsIdentity

北战南征 提交于 2019-11-27 09:52:41
问题 I want to protect ASP.NET Core Web API using JWT. Additionally, I would like to have an option of using roles from tokens payload directly in controller actions attributes. Now, while I did find it out how to use it with Policies: Authorize(Policy="CheckIfUserIsOfRoleX") ControllerAction()... I would like better to have an option to use something usual like: Authorize(Role="RoleX") where Role would be automatically mapped from JWT payload. { name: "somename", roles: ["RoleX", "RoleY", "RoleZ"

Data models not showing up in HttpResponseMessage

本小妞迷上赌 提交于 2019-11-27 09:34:25
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 bindingModel) { List<SomeModel> resultObjects; [...] // populate resultObjects with data return Request