asp.net-core-webapi

ASP.NET Core (2.1) Web API: Identity and external login provider

丶灬走出姿态 提交于 2019-12-21 12:36:06
问题 I have been discovering a bit the ASP.NET Core for a few days and wanted to try implementing authentication via LinkedIn. Most of the tutorials I found online used MVC and this is a seamless process, but I wanted to do a pure API. I've got something working, but I am not sure this is secure (I've hit some issue that I 'fixed' in a way that may be insecure). Context of the application: I am developing a Web API, with JWT Bearer token based authorization, which means I don't have any cookie

Custom authorization attribute in .NET Core [duplicate]

杀马特。学长 韩版系。学妹 提交于 2019-12-21 12:14:24
问题 This question already has answers here : How do you create a custom AuthorizeAttribute in ASP.NET Core? (8 answers) Closed 2 years ago . I'm building an API in .NET Core 1.1. I build a custom User object from HttpContext.User in a base controller that all of my other controllers inherit from, and I have authentication enabled by default (must be manually disabled with [AllowAnonymous] when necessary). The User object has an IsAdmin property. Right now I'm checking if the user is an admin at

How to change the default controller and action in ASP.NET Core API?

淺唱寂寞╮ 提交于 2019-12-21 04:35:15
问题 I'm creating an ASP.NET Core API app, and currently, and when one creates a new project there is a controller named Values, and by default the API opens it when you run. So, I deleted that controller and added a new controller named Intro, and inside it an action named Get. In the Startup.cs file, I have the following lines of code: app.UseMvc(opt => { opt.MapRoute("Default", "{controller=Intro}/{action=Get}/{id?}"); }); And my Intro controller looks like this: [Produces("application/json")]

In Web API.Core, how to do Model Validation in every method?

天大地大妈咪最大 提交于 2019-12-21 04:02:51
问题 I am getting into ASP.Net Core 2.0 with Web API. One of my first methods are my login: /// <summary> /// API endpoint to login a user /// </summary> /// <param name="data">The login data</param> /// <returns>Unauthorizied if the login fails, The jwt token as string if the login succeded</returns> [AllowAnonymous] [Route("login")] [HttpPost] public IActionResult Login([FromBody]LoginData data) { var token = _manager.ValidateCredentialsAndGenerateToken(data); if (token == null) { return

net::ERR_INVALID_HTTP_RESPONSE error after post request with Angular 7

China☆狼群 提交于 2019-12-21 03:52:12
问题 I am working on small web application and I am using angular 7 and asp.net core web api as a back end. I am trying to make http post request with angular. My service returns string (token) and when I receive it, I want to show it in alert box. I have tested my service with Postman and everything works as expected. From the browser my request is successfully mapped to the controller's action method. I have set breakpoint in this method body and it successfully returns token without any problem

How do I get current user in .NET Core Web API (from JWT Token)

南笙酒味 提交于 2019-12-20 09:28:16
问题 After a lot of struggling (and a lot of tuturials, guides, etc) I managed to setup a small .NET Core REST Web API with an Auth Controller issuing JWT tokens when stored username and password are valid. The token stores the user id as sub claim. I also managed to setup the Web API to validate those tokens when a method uses the Authorize annotation. app.UseJwtBearerAuthentication(...) Now my question: How do I read the user id (stored in the subject claim) in my controllers (in a Web API)? It

404 error when making a POST call from Angular HttpClient to ASP.NET Core 2.2 Web API (with CORS enabled)

我们两清 提交于 2019-12-20 07:36:37
问题 I've written a simple ASP.NET Core 2.2 Web API . The POST method always returns a 404 , but GET requests succeed. public class TestPayload { public string test1 { get; set; } public string test2 { get; set; } } [Route("api/[controller]")] [ApiController] public class TestController: ControllerBase { // POST api/create [HttpPost] public async Task<ActionResult<string>> Create([FromBody] TestPayload value) { return Ok(""); } } I get back a 404 error in my Angular HttpClient front-end. let

404 error when making a POST call from Angular HttpClient to ASP.NET Core 2.2 Web API (with CORS enabled)

六眼飞鱼酱① 提交于 2019-12-20 07:34:05
问题 I've written a simple ASP.NET Core 2.2 Web API . The POST method always returns a 404 , but GET requests succeed. public class TestPayload { public string test1 { get; set; } public string test2 { get; set; } } [Route("api/[controller]")] [ApiController] public class TestController: ControllerBase { // POST api/create [HttpPost] public async Task<ActionResult<string>> Create([FromBody] TestPayload value) { return Ok(""); } } I get back a 404 error in my Angular HttpClient front-end. let

Redirect to action after finishing background task queue

佐手、 提交于 2019-12-20 06:23:03
问题 I'm working on a .Net core solution that takes backup of storage files from another microservice and because this process takes too long time, we decided to build this routine under a background task.By following this link: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-2.1 I have implemented the background by using Queued background tasks like the following : public interface IBackgroundTaskQueue { void QueueBackgroundWorkItem(Func

where can be stored user info in .net core API project with angular

ぃ、小莉子 提交于 2019-12-20 05:49:21
问题 I have an angular project and i am using .net core 2.o Web API. I stored my user info in Jwt and i want log every database operation. I can access user info by sending jwt and taking from request.header in server side. But the problem is, where can be stored? In my old mvc projects, i could stored in session. But this project is API . And we work with JWT not session. How can i achieve that store UserInfo during the request start to end. And i want to access UserInfo from everywhere. This is