简体   繁体   English

通过 Azure AD 使用客户端凭据保护 API

[英]Securing API with client credentials via Azure AD

I have an ASP.NET Core API hosted in Azure which is to be accessed by several trusted clients.我有一个托管在 Azure 中的 ASP.NET Core API,可供多个受信任的客户端访问。 I wish to offer an /auth endpoint which accepts a client_id and client_secret.我希望提供一个接受 client_id 和 client_secret 的 /auth 端点。 Response will be an OAuth access token with expiry.响应将是过期的 OAuth 访问令牌。

The many examples/tutorials I have found mostly relate to username/password login and full OAuth flow (B2C) which isn't what I'm looking for as the trusted clients have the secret.我发现的许多示例/教程主要与用户名/密码登录和完整的 OAuth 流程 (B2C) 相关,这不是我正在寻找的,因为受信任的客户端拥有秘密。

I've been looking at Azure API Management which links through to Azure AD for OAuth but I'm thinking this is just complicating things right now.我一直在研究 Azure API 管理,它链接到用于 OAuth 的 Azure AD,但我认为这现在只是使事情复杂化。

In the past I have generated and validated JWT bearer tokens using the ASP.NET middleware, but I am sure I should be generating and validating tokens via Azure AD - or am I wrong here?过去,我使用 ASP.NET 中间件生成和验证了 JWT 不记名令牌,但我确信我应该通过 Azure AD 生成和验证令牌 - 还是我错了?

[expecting to get some down votes for not being explicitly code related, but really need a little bit of advice to get me past this] [希望得到一些不明确的代码相关的反对票,但真的需要一些建议来让我通过这个]

I wish to offer an /auth endpoint which accepts a client_id and client_secret.我希望提供一个接受 client_id 和 client_secret 的 /auth 端点。 Response will be an OAuth access token with expiry.响应将是过期的 OAuth 访问令牌。

You can use client credential flow .您可以使用客户端凭据流 Then you can use this endpoint to get access token.然后您可以使用此端点获取访问令牌。

POST /{tenant}/oauth2/v2.0/token HTTP/1.1           //Line breaks for clarity
Host: login.microsoftonline.com
Content-Type: application/x-www-form-urlencoded

client_id=535fb089-9ff3-47b6-9bfb-4f1264799865
&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default
&client_secret=qWgdYAmab0YSkuL1qKv5bPX
&grant_type=client_credentials

Before doing this, you need to expose application permissions of your application api.在执行此操作之前,您需要公开您的应用程序 api 的应用程序权限 The contents of appRoles are as below. appRoles的内容如下。

"appRoles": [
    {
    "allowedMemberTypes": [ "Application" ],
    "description": "Accesses the TodoListService-Cert as an application.",
    "displayName": "access_as_application",
    "id": "ccf784a6-fd0c-45f2-9c08-2f9d162a0628",
    "isEnabled": true,
    "lang": null,
    "origin": "Application",
    "value": "access_as_application"
    }
],

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

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