简体   繁体   English

如何在Asp Net Core RC1 MVC中添加带有令牌认证的“ApiController”

[英]How to add “ApiController” with token authentication inside Asp Net Core RC1 MVC

I know that this question could receive some downvotes, but I'm searching on Google for three days, with no really usable results. 我知道这个问题可能会收到一些downvotes,但是我在Google上搜索了三天,没有真正可用的结果。

I've created a default ASP.NET Core 1.0 RC1 project in Visual Studio, with Individual User Account authentication/authorization. 我在Visual Studio中创建了一个默认的ASP.NET Core 1.0 RC1项目,具有个人用户帐户身份验证/授权。 Everything is easy and simple this way, because the project is scaffolded using Microsoft.AspNet.Authentication.Cookies , and VS 2015 does all the heavy lifting. 这一切都很容易和简单,因为该项目是使用Microsoft.AspNet.Authentication.Cookies搭建的,VS 2015完成了所有繁重的任务。

However, we want to expose an API from the same project (using the same database, users, claims and so on), that will be consumed from mobile devices and even some simple SPA's. 但是,我们希望从同一个项目(使用相同的数据库,用户,声明等)公开API,这些API将从移动设备甚至一些简单的SPA中使用。 This way, we need to use something like JWT for the API (There are a bunch of tutorials on how to do this with WebAPI only). 这样,我们需要为API使用类似JWT的东西(有很多关于如何使用WebAPI执行此操作的教程)。

We want to do the main project using MVC (not SPA) way to leverage the use of view/controller scaffolding and everything Visual Studio can offer. 我们希望使用MVC(而不是SPA)方式来执行主项目,以利用视图/控制器脚手架以及Visual Studio可以提供的所有内容。

There is a lot of of tutorials of MVC-only or WebAPI-only approaches, but could you point me on how could I mix them together? 有许多仅限MVC或仅支持WebAPI的教程,但您能指出我如何将它们混合在一起?

Is there a way of using JWT only with MVC and WebAPI system-wide? 有没有办法只在整个系统范围内使用JWT和MVC和WebAPI?

Thank you in advance. 先感谢您。

Limiting identity by scheme this link may be answer of your question. 通过计划限制身份这个链接可能是你的问题的答案。

In some scenarios, such as Single Page Applications it is possible to end up with multiple authentication methods, cookie authentication to log in and bearer authentication for javascript request. 在某些情况下,例如单页应用程序,最终可能会有多种身份验证方法,用于登录的cookie身份验证以及用于javascript请求的承载身份验证。

I think, you can use below code for mvc and javascript calls(of course, you must enable cookie and bearer middlewares): 我想,您可以使用以下代码进行mvc和javascript调用(当然,您必须启用cookie和bearer中间件):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM