asp.net-core-webapi

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

浪尽此生 提交于 2019-11-29 14:34:54
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.UseAuthentication(); app.MessageHandlerMiddleware(); app.UseMvc(); I am new to .net web API implementation. Could

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

别来无恙 提交于 2019-11-29 14:20:56
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 result code. I would prefer to not handle this in a custom AuthorizeAttribute but would rather handle at

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

隐身守侯 提交于 2019-11-29 12:58:53
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 Ingnection container and other .Net Core new functionality Will this be aniway possible by targeting old

How to get content value in Xunit when result returned in IActionResult type

六月ゝ 毕业季﹏ 提交于 2019-11-29 11:08:30
问题 I have a unit test project using Xunit and the method we are testing returns IActionResult . I saw some people suggest using "NegotiatedContentResult" to get the content of the IActionResult but that doesn't work in Xunit. So I wonder how to get the content value of an IActionResult in Xunit? Test code example is provided below: public void GetTest() { var getTest = new ResourcesController(mockDb); var result = getTest.Get("1"); //Here I want to convert the result to my model called Resource

ASP.NET Core WebAPI default route not working

守給你的承諾、 提交于 2019-11-29 09:31:00
I've followed several examples suggesting that to set my default route in an ASP.NET Core WebAPI project, I need to replace app.UseMvc(); with app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller}/{action}", defaults: new { controller = "Traders", action = "Get" }); }); But when I run it defaults to localhost:54321/api/values and it should default to localhost:54321/Traders What's wrong? As @tmg mentioned, do the following: Right click your web project -> Select Properties -> Select the Debug tab on the left -> Then edit the 'Launch Url' field to set your own default

Registering a new DelegatingHandler in ASP.NET Core Web API

烈酒焚心 提交于 2019-11-29 03:01:52
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); } } This is all fine and dandy except it doesn't do anything because I haven't registered it anywhere!

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

守給你的承諾、 提交于 2019-11-29 02:04:34
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) { return "value"; } // POST api/values [HttpPost] public void Post([FromBody]string value) { } // PUT api

Return jpeg image from Asp.Net Core WebAPI

被刻印的时光 ゝ 提交于 2019-11-29 00:59:51
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 below [HttpGet] public async Task<HttpResponseMessage> Get() { var image = System.IO.File.OpenRead("C:

ASP.NET Core 2.0 JWT Validation fails with `Authorization failed for user: (null)` error

眉间皱痕 提交于 2019-11-29 00:57:11
I'm using ASP.NET Core 2.0 application (Web API) as a JWT issuer to generate a token consumable by a mobile app. Unfortunately, this token couldn't be validated by one controller while can be validated by another (using the same validation setting within the same asp.net core 2.0 app). So I have a token which is valid and could be decoded, has all the required claims and timestamps. But one endpoint accepts it, while another gives me 401 error and debug output: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService:Information: Authorization failed for user: (null). [40m[32minfo[39m

How to compile .NET Core app for Linux on a Windows machine

为君一笑 提交于 2019-11-29 00:51:53
问题 I'm developing a .NET Core app on a Windows 10 machine (with Visual Studio 2015 update 3 + Microsoft .NET Core 1.0.1 VS 2015 Tooling Preview 2) which should published on an Ubuntu 16 machine. To do that, I have to move my source code to the end machine and compile it there, to get it to run. e.g. I'm not able to compile the code on windows and run it on linux. Question: Is there any way to compile the code on win machine and run it on linux? 回答1: Using dotnet build command, you may specify -