asp.net-core-webapi

Mock IHttpContextAccessor in Unit Tests

℡╲_俬逩灬. 提交于 2020-04-29 08:51:20
问题 I have a method to get header value using IHttpContextAccessor public class HeaderConfiguration : IHeaderConfiguration { public HeaderConfiguration() { } public string GetTenantId(IHttpContextAccessor httpContextAccessor) { return httpContextAccessor.HttpContext.Request.Headers["Tenant-ID"].ToString(); } } I am testing GetBookByBookId method Let's say the method looks like this: public class Book { private readonly IHttpContextAccessor _httpContextAccessor; private IHeaderConfiguration

ASP.NET Core Web Api Error for IhttpActionResult

你说的曾经没有我的故事 提交于 2020-04-18 05:35:07
问题 I am trying to create a new Core Web API app for the first time. I am using Core 2.2 I did some research but did not find the correct answer. Not sure if I am using wrong libraries. cannot implicitly convert type microsoft.aspnetcore.mvc.okresult to system.web.http.Ihttpactionresult Here is my Code using System.Threading.Tasks; using System.Web.Http; using Microsoft.AspNetCore.Mvc; [Microsoft.AspNetCore.Mvc.Route("api/User")] [ApiController] public class UserController : ControllerBase {

Overriding ModelBindingMessageProvider error messages

可紊 提交于 2020-04-18 03:49:24
问题 I have a .net core 2.2 webapi. There is a POST action that accepts a model. The model has a Guid as one of the properties. When I post this model but supply a string and not a Guid, I get a ModelState.IsValid = false , which is correct. The default model binding error message is "Error converting value \"string\" to type 'System.Guid'. Path 'memberId', line 3, position 22." This is not a friendly message that I want to return, also, this message needs to get localized to user's language. All

Simple way to check for api key in Web API ASP.NET Core

一曲冷凌霜 提交于 2020-04-16 03:27:28
问题 I'd like to simply check for an Api Key — sent up in the Authorization header — prior to allowing certain Web API endpoints from getting hit. For the sake of this question, let's assume the ApiKey is 12345 . I just want to check the value of this Api Key prior to reaching the specific action method. I can't figure out whether or not this calls for a custom AuthorizeAttribute or an action filter. 回答1: Simply, I make a request GET with header is Authorization: apiKey 12345 The authorization

How to Validate OpenID Connect Access Token generated by Azure AD v2 in ASP.NET core WEB API?

五迷三道 提交于 2020-04-14 09:05:11
问题 How to Validate OpenID Connect Access Token generated by Azure AD (v2!!!) in ASP.NET core WEB API? The Scenario is: I have an Angular 8 Client Application which is getting an OpenID Connect access Token after Login. The Client can call the API along with the Access Token. But Question is, How should I validate that Token in ASP.NET core API? With this code I get an Authorize Error without any descriptions. services.AddAuthentication(AzureADDefaults.BearerAuthenticationScheme)

How to overload controller methods with same number of arguments in ASP.NET Core Web API?

断了今生、忘了曾经 提交于 2020-04-13 17:42:23
问题 I'm migrating a full .NET Framework Web API 2 REST project over to ASP.NET Core 2.2 and getting a bit lost in the routing. In Web API 2 I was able to overload routes with the same number of parameters based on the parameter type, e.g. I could have Customer.Get(int ContactId) and Customer.Get(DateTime includeCustomersCreatedSince) and incoming requests would be routed accordingly. I haven't been able to achieve the same thing in .NET Core, I either get a 405 error or a 404 and this error

How to log from .NET Core Web API into Elasticsearch on own index

▼魔方 西西 提交于 2020-04-10 08:19:13
问题 I have a .NET Web API written with C# and a Elasticsearch . On the Elasticsearch I have a index " logging " where I want to push my logs from the API into. I can not figure out how to get my logs from the C# API into the Elastic "logging". I read documentations like Logging with ElasticSearch..., but I have no logstash available at my Elasticsearch. So I'm searching for a Package which helps my logging in a easy way. I think need to hand over the Index "logging" ones, so it knows where to log

.NET Core RuntimeIdentifier vs TargetFramework

随声附和 提交于 2020-04-07 18:41:11
问题 Can someone explain the purpose of this two in csproj file (VS2017): <TargetFramework>netstandard1.6</TargetFramework> <RuntimeIdentifier>win7</RuntimeIdentifier> I just migrated from VS2015 and now can't publish my web api because it looks I should use only one target framework. In addition I can't specify multiple RIDs. All these changed things make me frustrated. Nothing works from scratch, should overcome something over and over. I just want developing my web-api on windows, run xUnit

Azure AD Multi Tenant ,.Net Core Web API with JWT Token

妖精的绣舞 提交于 2020-03-22 12:53:18
问题 So I raised this question Azure AD Multi Tenant ,.Net Core Web API with MSAL(Microsoft Authentication Libary) which showed me how to turn on the error output. Why would I be getting this now with my token, at a guess is the aud right and the iss for a multi tenant ad token? AuthenticationFailed: IDX10511: Signature validation failed. Keys tried: 'Microsoft.IdentityModel.Tokens.X509SecurityKey, KeyId: 'YMELHT0gvb0mxoSDoYfomjqfjYU', InternalId: '2c34a300-21bb-4eb1-b3b9-1944f1be7470'. , KeyId:

How do I resolve the issue the request matched multiple endpoints in .Net Core Web Api

寵の児 提交于 2020-03-19 06:01:38
问题 I notice that there are a bunch of similar questions out there about this topic. I'm getting this error when calling any of the methods below. Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException: The request matched multiple endpoints. I can't however sort out what is best practice in resolving the issue. So far I haven't set up any specific routing middleware. // api/menus/{menuId}/menuitems [HttpGet("{menuId}/menuitems")] public IActionResult GetAllMenuItemsByMenuId(int menuId) {