c# - 使用 Azure AD 客户端凭据授予流程的 Azure Devops Access

标签 c# azure oauth-2.0 azure-devops azure-active-directory

我已成功通过 Azure AD 进行身份验证并收到访问 token 。我已授予 Azure AD 应用 API 访问 Azure DevOps 的权限

我正在使用 RestSharp Http 客户端进行身份验证

var client = new RestClient("https://login.microsoftonline.com/{tenant}/oauth2/token");
            var request = new RestRequest("", Method.POST);
            request.AddParameter("grant_type", "client_credentials");
            request.AddParameter("client_id", "00000f-0000-00-00-000000");
            request.AddParameter("client_secret", "][M.&*******?*_5z)y${*[)");
            request.AddParameter("resourse", "https://tenant.onmicrosoft.com/4815c06b-7e28-4f88-9dc8-8fe3354d5909");
            IRestResponse response = client.Execute(request);
            var content = response.Content; // raw con

到目前为止我很高兴。我不知道如何使用访问 token 访问 Azure DevOps 到目前为止我已经尝试过

 var client = new RestClient("https://app.vssps.visualstudio.com/oauth2/token");
        var request = new RestRequest("", Method.POST);
        request.AddParameter("client_id", "My APP code here";
        request.AddParameter("client_secret", "My ap secret here");
        request.AddParameter("client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer");
        request.AddParameter("client_assertion", "access token here");
        request.AddParameter("grant_type", "authorization_code");

我收到的错误是“{\”Error\”:\“unsupported_grant_type\”,\“ErrorDescription\”:\“grant_type必须是ietf jwt-bearer类型或refresh_token\”}“

我错过了什么?

最佳答案

据我所知,不支持客户端凭据。 如果我们想使用Azure AD凭证访问Azure Devops API,我们需要创建一个Azure AD native 应用程序并为Azure AD应用程序分配权限

enter image description here

之后我们还需要connect the Azure Devepops account to the Azure AD.

在浏览器中输入地址并输入用户名和密码即可获取authorization_code

获取授权码

https://login.microsoftonline.com/{tenantId}/oauth2/authorize?resource=499b84ac-1321-427f-aa17-267ca6975798&client_id={applicationId}&response_type=code&redirect_uri={redirecturl}

获取访问 token

POST login.microsoftonline.com/e4c9ab4e-bd27-40d5-8459-230ba2a757fb/oauth2/token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Body
resource=499b84ac-1321-427f-aa17-267ca6975798&client_id={clientid}&grant_type=authorization_code&code=AQABAAIAAA..&redirect_uri={redirectUrl}

enter image description here

然后我们可以使用访问 token 来访问 Azure Devops API。

我们可以从这个link获得更多详细步骤和C#演示代码.

关于c# - 使用 Azure AD 客户端凭据授予流程的 Azure Devops Access,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53466292/

相关文章:

c# - WinRT XAML 上的 Image.OpacityMask

C# - 非静态类上的静态事件

Azure Batch REST API,授权问题

Azure Function ExecutionContext.InitationId 与 FunctionFilterContext.FunctionInstanceId

java - 更正应用程序的类路径,使其包含单个兼容版本的 oauth2.client.registration.ClientRegistrations

来自 OAuth2 playground 的 Firebase 不记名 token

c# - 如何在没有管理员权限的情况下安装 chrome-frame?

c# - 使用 ffmpeg 时如何提高图像导出/提取速度?

使用 ADAL 的 Azure AD OAuth 身份验证 : what's the resource ID?

java - 确保后端服务安全的正确方法 - oAuth