asp.net-core-webapi

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

两盒软妹~` 提交于 2019-11-28 20:07:24
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? tmacharia You can just hi-jack the HttpContext from the incoming Http Request and add your own custom headers to the Response object before calling return. If you want your custom header to persist and be added in all API requests across multiple

ASP.NET Core JWT mapping role claims to ClaimsIdentity

二次信任 提交于 2019-11-28 16:44:14
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"] } So, what is the easiest way to accomplish this in ASP.NET Core? Is there a way to get this working

ASP.NET Core web api action selection based on Accept header

一曲冷凌霜 提交于 2019-11-28 10:02:12
I want to return two different formatted responses for the same feature (a list of entities) based on the accept header of the request, it is for a "json" and a "html" request. Does the asp.net core support select different actions for the same route based upon the Accept Header from the request? I dived into the .net core source code and looked for other attributes that do some similar behaviour such as Microsoft.AspNetCore.Mvc.HttpGet or Microsoft.AspNetCore.Mvc.ProducesAttribute . Both attributes implements an Microsoft.AspNetCore.Mvc.ActionConstraints.IActionConstraint interface wich is

How do I customize ASP.Net Core model binding errors?

扶醉桌前 提交于 2019-11-28 10:01:34
I would like to return only standardized error responses from my Web API (Asp.net Core 2.1), but I can't seem to figure out how to handle model binding errors. The project is just created from the "ASP.NET Core Web Application" > "API" template. I've got a simple action defined as: [Route("[controller]")] [ApiController] public class MyTestController : ControllerBase { [HttpGet("{id}")] public ActionResult<TestModel> Get(Guid id) { return new TestModel() { Greeting = "Hello World!" }; } } public class TestModel { public string Greeting { get; set; } } If I make a request to this action with an

How to validate JWT Token in aspnet.core web api?

随声附和 提交于 2019-11-28 08:08:04
问题 I have created custom middleware class which validates the JWT token. I am calling this method before app.AddMvc() in configure method. *** I would like to know what are the things that I should add to Configuration services to authenticate my web API using JWT? I have added [Authorize] in my Controller class Do I need to call my middleware class which validates the JWT token first in Configure method? or I should call App.UseAuthentication() I am using the following order : app

Disable Not Authorized Redirect to Account/Login in ASP.NET Core

旧街凉风 提交于 2019-11-28 07:51:07
问题 I have a set of WebAPI services in a shared library. These are used in an ASP.NET Core MVC Web Site and dedicated server only hosting the WebAPI Services without the MVC component. Everything works as expected on the MVC Web Site with Unauthorized Requests, I get the 304 redirect to the login page (Account/Login). However when I make an unauthorized request to the WebAPI services, I receive the same 304 redirect to /Account/Login in this case I would like to return the Http 401 Unauthorized

How to add additional claims to be included in the access_token using ASP.Net Identity with IdentityServer4

99封情书 提交于 2019-11-28 07:03:16
How to add additional claims to be included within the token? As soon as the API receives the bearer token, the User.Identity object gets populated with the following claims. [ { "key": "nbf", "value": "1484614344" }, { "key": "exp", "value": "1484615244" }, { "key": "iss", "value": "http://localhost:85" }, { "key": "aud", "value": "http://localhost:85/resources" }, { "key": "aud", "value": "WebAPI" }, { "key": "client_id", "value": "MyClient" }, { "key": "sub", "value": "d74c815a-7ed3-4671-b4e4-faceb0854bf6" }, { "key": "auth_time", "value": "1484611732" }, { "key": "idp", "value": "local" },

Referencing old (full .NET Framework) Class Library when targeting .NET Core

一曲冷凌霜 提交于 2019-11-28 06:41:35
问题 I'm developing a web application on .Net Core, but as .Net Core is still on development some libraries used are not built on Core yet. I know I can target .Net 4.6 in my application to use old libraries but I'm not sure about what actually change in the use of new .Net Core functionality inside my app. I know this way I'm loosing multi platform capabilities but I actually don't need it at the moment insead I need going on using API and MVC unified pipeline, the integrated dependency

ASP.NET Core with IIS - HTTP Verb Not Allowed

て烟熏妆下的殇ゞ 提交于 2019-11-28 06:19:21
We have an ASP.NET Core 2.0 web site that also presents a couple of simple Web API methods for UI enhancement purposes. The Web API calls work as expected when running locally under IIS Express , but when we deploy to our IIS 8.5 production web server, we get the following error when making HTTP DELETE and PUT requests... 405 Method Not Allowed After some web searching, we have found several posts suggesting the removal of the IIS WebDAV module. We have disabled this in IIS (it is our server), and we have also tried the following: Disabled WebDAV Enabled WebDev and set Allow verb filtering =

.Net Core 2.0 Web API using JWT - Adding Identity breaks the JWT authentication

旧巷老猫 提交于 2019-11-28 04:45:17
(Edit - Found proper fix! see below) OK - this is my first attempt at .Net Core 2.0 and authentication, though I've done things with Web API 2.0 in the past, and have worked fairly extensively on various MVC and Webforms ASP projects over the last couple of years. I'm trying to create a Web API ONLY project using .Net Core. This will form the back end of a multi-tenant application for producing some reports, so I need to be able to authenticate users. It seems the usual approach is to use JWT - first authenticate the user to generate a token, then pass that to the client to use on every API