简体   繁体   English

使用EWS托管API的Web API的正确OAuth2身份验证流程

[英]Proper OAuth2 authentication flow for a web API using the EWS Managed API

I've been reading through a bunch of documentation for using OAuth with Azure AD, but am still completely confused about how to properly implement things for my situation. 我一直在阅读一堆有关将OAuth与Azure AD一起使用的文档,但是对于如何正确实现我的情况仍然感到困惑。 Hopefully someone can steer me in the right direction. 希望有人可以指引我正确的方向。

I have created an ASP.NET Web API application that uses the EWS Managed API to access Exchange on behalf of different users. 我创建了一个ASP.NET Web API应用程序,该应用程序使用EWS托管API代表不同的用户访问Exchange。 My application exposes endpoints such as /Mailbox/Messages and /Appointments with the intent that some front end web application will eventually use them to retrieve a user's emails and appointments. 我的应用程序公开了端点,例如/Mailbox/Messages/Appointments ,目的是某些前端Web应用程序最终将使用它们来检索用户的电子邮件和约会。 Currently the endpoints are working using basic http authentication, but I'd like to update them to use OAuth. 目前,端点正在使用基本的http身份验证,但是我想将其更新为使用OAuth。 The application has been registered in my Azure AD instance and I've configured it to require the "Access mailboxes as the signed-in user via Exchange Web Services" API permission. 该应用程序已在我的Azure AD实例中注册,并且已将其配置为要求“通过Exchange Web Services以登录用户身份访问邮箱” API权限。

Since the front end hasn't been implemented yet, I've been trying to test by manually calling the authentication endpoint. 由于尚未实现前端,因此我一直在尝试通过手动调用身份验证终结点进行测试。 This prompts me to log in and provide consent. 这提示我登录并提供同意。 If I consent, I'm redirected to the callback URL that I provided when I registered the app with the authorization code contained in the query parameters. 如果我同意,我将重定向到使用查询参数中包含的授权代码注册应用程序时提供的回调URL。 I'm still not quite sure how I'm supposed to be using this callback, but for the sake of testing I currently have the callback redeem the authorization code for an access token. 我仍然不太确定应该如何使用此回调,但是为了进行测试,我目前使该回调赎回了访问令牌的授权代码。 This is done by calling the AcquireTokenByAuthorizationCode method on an instance of the AuthenticationContext class and providing my application's id and secret. 这是通过在AuthenticationContext类的实例上调用AcquireTokenByAuthorizationCode方法并提供我的应用程序的ID和密码来完成的。 Again, just for the sake of testing I return the access token to the browser. 同样,仅出于测试目的,我将访问令牌返回到浏览器。 I can then call my aforementioned endpoints (after some modifications) with this access token and get the emails for the user. 然后,我可以使用此访问令牌调用上述端点(经过一些修改),并获取该用户的电子邮件。 I'm guessing much of this is not the correct way to be doing things. 我猜这很多不是做事的正确方法。

Some of my points of confusion: 我的一些困惑点:

  1. What should the callback that I registered in Azure AD actually be doing when it gets the authorization code? 当我获得授权代码时,我在Azure AD中注册的回调实际上应该做什么? Is this intended for a different type of application? 这是针对其他类型的应用程序吗? Perhaps one that isn't just playing the role of a middle man. 也许不仅仅是扮演中间人的角色。
  2. I'm trying to make my application somewhat RESTful, so I don't want to have to maintain the access tokens on my end between requests. 我试图使我的应用程序具有某种RESTful的风格,所以我不想在两次请求之间都维护访问令牌。 As such, does it make sense for my endpoints to expect that the access token be provided in the authentication header for each request? 这样,对于我的端点来说,期望在身份验证标头中为每个请求提供访问令牌是否有意义? If so, does that mean the front end application should be responsible acquiring the access token and passing it to me? 如果是这样,这是否意味着前端应用程序应负责获取访问令牌并将其传递给我?

Being completely new to OAuth and Azure, I'm not sure if any other details are pertinent, but I can provide more information as needed. 对于OAuth和Azure来说是全新的,我不确定是否还有其他详细信息,但是我可以根据需要提供更多信息。

What you are implementing is this scenario: https://docs.microsoft.com/en-us/azure/active-directory/active-directory-authentication-scenarios#daemon-or-server-application-to-web-api 您正在实现的是这种情况: https : //docs.microsoft.com/zh-cn/azure/active-directory/active-directory-authentication-scenarios#daemon-or-server-application-to-web-api

Here's how it works: 运作方式如下:

  1. Your client app redirects the user to sign in at the authorization endpoint 您的客户端应用重定向用户以在授权端点上登录
  2. Your client app gets back an authorization code (if using the auth code grant flow, there are others) 您的客户端应用会获取授权码(如果使用授权码授予流程,则还有其他授权码)
  3. The client app exchanges the code for an access token for your API app 客户端应用程序将代码交换为API应用程序的访问令牌
    1. It will need to provide its client id and secret along with the code and the API's resource URI to get it 它将需要提供其客户端ID和密码以及代码和API的资源URI才能获取它
  4. The client app calls to your API app, passing the access token in the Authorization header 客户端应用调用您的API应用,并在Authorization标头中传递访问令牌
  5. Your API app then validates the access token, and requests for another access token from Azure AD for the Exchange API 然后,您的API应用将验证访问令牌,并从Azure AD请求另一个 Exchange Exchange API的访问令牌
    1. It will pass the access token sent by the client app, along with its client id and secret and the Exchange API's resource URI to Azure AD 它将客户端应用程序发送的访问令牌及其客户端ID和密钥以及Exchange API的资源URI传递给Azure AD
  6. Your API app receives an access token so you can call to the Exchange API as the user 您的API应用会收到访问令牌,因此您可以以用户身份调用Exchange API

And to answer your two questions: 并回答您的两个问题:

  1. Authorization code flow is not used with APIs, only with apps that have a user signing in, thus the redirect URL is basically never used 授权代码流不用于API,仅用于具有用户登录权限的应用,因此,基本上从不使用重定向URL
  2. Your API can and must expect and authenticate the access token for it to be in every request. 您的API可以而且必须期望并验证访问令牌,以便可以出现在每个请求中。 But the access token it uses to call the Exchange API can and should be cached on the API's side. 但是,它用来调用Exchange API的访问令牌可以并且应该在API端进行缓存。 This is provided out-of-the-box with ADAL, though the tokens are only in memory. 尽管令牌仅在内存中,但它随ADAL一起提供。

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

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