简体   繁体   English

ASP.NET MVC + Web API2 + AngularJS授权和身份验证

[英]ASP.NET MVC + Web API2 + AngularJS authorization and authentication

I'm developing one large application, which will consist of both ASP.NET MVC and AngularJS+Web Api2 on back-end. 我正在开发一个大型应用程序,它将包含后端的ASP.NET MVC和AngularJS + Web Api2。 Previously I have used MVC forms authorization in my applications. 以前我在我的应用程序中使用过MVC表单授权。 There are lots of examples how to use token based authorization with AngularJS+Web Api, but can't find complete solution that will fit both cases. 有很多例子如何使用AngularJS + Web Api进行基于令牌的授权,但找不到适合这两种情况的完整解决方案。

Please provide me some information, where I can read on this topic, maybe step-by-step examples, tutorials, or blogs, where mixed authorization for both is described. 请向我提供一些信息,我可以在这里阅读这个主题,可能是分步示例,教程或博客,其中描述了两者的混合授权。

  1. Implement ASP.Net Identity 2 in your WebAPI2 application here is a good tutorial on implementing ASP.Identity 2 . 在WebAPI2应用程序中实现ASP.Net Identity 2 是一个很好的实现ASP.Identity 2的教程 It's better if you keep the WebAPI and MVC5 app in different projects. 如果您将WebAPI和MVC5应用程序保存在不同的项目中,那就更好了。

  2. Decide whether you like to connect to the API directly from your AngularJS app or from your MVC app 决定是否要直接从AngularJS应用程序或MVC应用程序连接到API

  3. If calling API methodes directly from your AngularJS app, use this directive for OAuth2 . 如果直接从AngularJS应用程序调用API方法,请将此指令用于OAuth2 Store the access_token in localstorage or cookie. 将access_token存储在localstorage或cookie中。

  4. If calling the API methodes from your inside your MVC app, store the access_token on the client side (using either localstorage or a cookie) and use it for each call to your API. 如果从您的MVC应用程序内部调用API方法,请将access_token存储在客户端(使用localstorage或cookie),并将其用于每次调用API。 here is a tutorial showing how to use AngularJS with ASP.Net MVC5 这是一个教程,展示如何将AngularJS与ASP.Net MVC5一起使用

  5. Make sure you are using a secure connection (https) in production. 确保在生产中使用安全连接(https)。

Update 更新

You can use the default the ASP.Net providers for WebAPI. 您可以使用默认的ASP.Net提供程序进行WebAPI。 But the default providers work best with EnityFramework. 但是默认提供程序最适合EnityFramework。
I am not a fan of EF so I used custom providers myself. 我不是EF的粉丝所以我自己使用自定义提供商。 Here is an overview of custom idenity providers. 以下是自定义标识提供程序的概述 This MySQL example helped me a lot in implementing a custom provider (however I still used SQL Server but with our own internal DAL). 这个MySQL示例帮助我实现了一个自定义提供程序(但我仍然使用SQL Server但使用我们自己的内部DAL)。

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

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