How to add "ApiController" with token authentication inside Asp Net Core RC1 MVC

I know this question may get some downvotes, but I’ve been searching on Google for three days without real results.

I created a default ASP.NET Core 1.0 RC1 project in Visual Studio with individual user account authentication / authorization. Everything is so simple and simple, because the project is built using Microsoft.AspNet.Authentication.Cookies , and VS 2015 is doing all the hard lifting.

However, we want to expose the API from the same project (using the same database, users, claims, etc.) that will be consumed from mobile devices and even using some simple SPA. Thus, we need to use something like a JWT for the API (there are many tutorials on how to do this using WebAPI).

We want to make the main project using MVC (not SPA) in order to use the use of visible / control forests and all that Visual Studio has to offer.

There are many tutorials for MVC only or WebAPI only, but could you please tell me how I could combine them together?

Is there a way to use JWT only with MVC and WebAPI system-wide?

Thanks in advance.

+5
source share
1 answer

Identity restriction according to the scheme, this link may be the answer to your question.

In some scenarios, such as Single Page Applications, you can end up with several authentication methods, cookie authentication for login, and bearer authentication for javascript request.

I think you can use the below code for mvc and javascript calls (of course, you should enable cookies and medium carriers):

 [Authorize(AuthenticationSchemes = "Cookie,Bearer")] public class YourController : Controller 
+1
source

Source: https://habr.com/ru/post/1247152/


All Articles