简体   繁体   English

带有WebAPI和MVC应用程序的AngularJS SPA。 如何使用OWIN设置授权

[英]AngularJS SPA with WebAPI and MVC application . How to setup Authorization with OWIN

I am a newbie in AngularJS and WebAPI, and I am looking to create a SPA template for my organization that becomes boiler plate for making quick SPAs involving : 我是AngularJS和WebAPI的新手,我希望为我的组织创建SPA模板,该模板将成为制作快速SPA的样板,其中包括:

  1. WebAPIs 2 - For purely data retrieval and insertion purposes and WebAPI 2-仅用于数据检索和插入目的,以及
  2. MVC 5 Controllers - For fetching the views. MVC 5控制器 -用于获取视图。 Basically, all GET requests and nothing else 基本上,所有GET请求都没有
  3. MVC Views : Partial Views that will be eventually loaded into ng-view placeholder MVC视图 :部分视图将最终加载到ng-view占位符中
  4. AngularJS Controllers - All MVC views will be tied to their respective individual Angular controllers. AngularJS控制器 -所有MVC视图将绑定到它们各自的单独Angular控制器。
  5. ASP.NET Identity 2 : For User Store and Roles ASP.NET Identity 2 :用于用户存储和角色
  6. OWIN Security : For token and cookie based authorization of my WebAPIs and MVC Controllers. OWIN安全性 :用于基于令牌和cookie的WebAPI和MVC控制器的授权。

In startup.Auth.cs. 在startup.Auth.cs中。 I am using following authorization options : 我正在使用以下授权选项:

app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOAuthBearerTokens(OAuthOptions);

I have created a small diagram to explain the flow of data 我创建了一个小图来解释数据流

在此输入图像描述

WebAPIs and MVC Controllers for now will sit in 1 tier, but architecture should allow them to be separated. 目前,WebAPI和MVC控制器将位于1层,但体系结构应允许将它们分开。

Now, my questions are 现在,我的问题是

  1. Is this architecture reasonable for building SPA 这种架构对于构建SPA是否合理?
  2. Would [Authorize] Attribute on my MVC controllers be able to recognize and decipher the cookie that WEBAPIs returned after authentication, considering that WebAPis and MVC app can be on two different tiers in future. 考虑到WebAPis和MVC应用程序将来可能位于两个不同的层上,我的MVC控制器上的[Authorize]属性是否能够识别和破译WEBAPI在身份验证后返回的cookie。

The [Authorize] attribute checks whether the IsAuthenticated property is set on the Request object that both MVC and Web API use. [Authorize]属性检查是否在MVC和Web API都使用的Request对象上设置了IsAuthenticated属性。 This property is set by the Identity middleware, as you configured in the startup.cs file. 该属性由身份中间件设置,如您在startup.cs文件中配置的那样。

Each request flows through the OWIN middleware pipeline before arriving at either an MVC or Web API controller. 每个请求在到达MVC或Web API控制器之前都要经过OWIN中间件管道。 Middleware can then alter, or even completely handle the request. 然后,中间件可以更改甚至完全处理该请求。 In essence, MVC and Web API are also middlewares themselves, saying "hey, if this url request matches this route, I'll handle it guys". 本质上,MVC和Web API本身也是中间件,他们说:“嘿,如果此url请求与该路由匹配,我会处理的。” ASP.NET Identity is a middleware which doesn't complete a request, but alters it before passing it on down the pipeline. ASP.NET Identity是一种中间件,它不会完成请求,但是会在将其传递到管道之前对其进行更改。 It checks for supplied credentials in the request (in your case, in the form of a bearer token for the web API or in the form of a cookie for MVC). 它检查请求中提供的凭据(在您的情况下,以Web API的承载令牌形式或MVC的cookie形式)。 If they are found, then the authentication details are added to the request object, which is then utilized by Web API or MVC to do the [Authorize] check. 如果找到它们,则将身份验证详细信息添加到请求对象,然后由Web API或MVC将该身份验证详细信息用于进行[Authorize]检查。

The reason that your app accepts only the tokens it has handed out itself is because it's encrypted with a key known only to the application itself. 您的应用仅接受自己发出的令牌的原因是,它使用仅应用本身知道的密钥进行了加密。 If you intend to accept tokens encrypted from a different application, these applications should use the same private key for encryption. 如果您打算接受从其他应用程序加密的令牌,则这些应用程序应使用相同的私钥进行加密。 You can configure these keys in the web.config. 您可以在web.config中配置这些密钥。 Be very careful not to lose these keys (for example, don't add your web.config with these details to a git repository). 注意不要丢失这些密钥(例如,不要将具有这些详细信息的web.config添加到git存储库中)。 Alternatively, you can set up a separate authorization server which mediates between the different applications relying on it. 另外,您可以设置一个单独的授权服务器,该服务器在依赖它的不同应用程序之间进行中介。

Hope this helps! 希望这可以帮助!

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

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