【问题标题】:How to get client_assertion string如何获取 client_assertion 字符串
【发布时间】:2018-01-02 08:52:03
【问题描述】:

我已经用 C++ 编写了 EWS 应用程序。目前支持Basic和NTLM认证,现在尝试支持OAuth认证

由于它是 C++ 应用程序,我不能使用 .NET AcquireToken,所以我需要发布以下 OAuth 身份验证请求

POST https://login.microsoftonline.com/b9bd2162xxx/oauth2/tokenHTTP/1.1

内容类型:application/x-www-form-urlencoded

resource=https://tailspin.onmicrosoft.com/surveys.webapi

&client_id=87df91dc-63de-4765-8701-b59cc8bd9e11

&client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer

&client_assertion=eyJhbGci...

&grant_type=authorization_code

所以我的问题是,如果我正在构建请求,我怎样才能获得 client_assertion 字符串?是否有任何 API\开源库可以使用 .pfx\X.509 证书获取此字符串?

【问题讨论】:

    标签: azure-active-directory adal microsoft-account


    【解决方案1】:

    根据grant_type 的值,您使用的是授权码授予流程。此流程用于交互式应用程序。如果您要使用此流程,则无需提供client_assertionclient_assertion_type

    您可以参考以下有关此流程的请求。

    1.请求授权码:

    https://login.microsoftonline.com/{tenant}/oauth2/authorize?
    client_id=6731de76-14a6-49ae-97bc-6eba6914391e
    &response_type=code
    &redirect_uri=http%3A%2F%2Flocalhost%2Fmyapp%2F
    &response_mode=query
    &resource=https%3A%2F%2Fservice.contoso.com%2F
    &state=12345
    

    2.使用授权码请求访问令牌:

    POST /{tenant}/oauth2/token HTTP/1.1
    Host: https://login.microsoftonline.com
    Content-Type: application/x-www-form-urlencoded
    grant_type=authorization_code
    &client_id=2d4d11a2-f814-46a7-890a-274a72a7309e
    &code=AwABAAAAvPM1KaPlrEqdFSBzjqfTGBCmLdgfSTLEMPGYuNHSUYBrqqf_ZT_p5uEAEJJ_nZ3UmphWygRNy2C3jJ239gV_DBnZ2syeg95Ki-374WHUP-i3yIhv5i-7KU2CEoPXwURQp6IVYMw-DjAOzn7C3JCu5wpngXmbZKtJdWmiBzHpcO2aICJPu1KvJrDLDP20chJBXzVYJtkfjviLNNW7l7Y3ydcHDsBRKZc3GuMQanmcghXPyoDg41g8XbwPudVh7uCmUponBQpIhbuffFP_tbV8SNzsPoFz9CLpBCZagJVXeqWoYMPe2dSsPiLO9Alf_YIe5zpi-zY4C3aLw5g9at35eZTfNd0gBRpR5ojkMIcZZ6IgAA
    &redirect_uri=https%3A%2F%2Flocalhost%2Fmyapp%2F
    &resource=https%3A%2F%2Fservice.contoso.com%2F
    &client_secret=p@ssw0rd
    
    //NOTE: client_secret only required for web apps
    

    有关此流程的更多详细信息,请参阅以下文档:

    Authorize access to web applications using OAuth 2.0 and Azure Active Directory

    更新

    string clientId = "";
    string thumbprint = "";
    X509Certificate2 cert = GetCertificate(thumbprint);
    string resource = "";
    
    string authority = "https://login.microsoftonline.com/{tenant}";
    AuthenticationContext authContext = new AuthenticationContext(authority);
    var resoult=  authContext.AcquireTokenAsync(resource, new ClientAssertionCertificate(clientId, cert)).Result;
    Console.WriteLine(resoult.AccessToken);
    

    【讨论】:

    • 我的 EWS 应用程序运行一个服务,没有与最终用户交互。在这种情况下,如何让 client_assertion 发布以下查询 POST login.microsoftonline.com/b9bd2162xxx/oauth2/token HTTP/1.1 Content-Type: application/x-www-form-urlencoded resource=tailspin.onmicrosoft.com/surveys.webapi &client_id=87df91dc-63de-4765-8701-b59cc8bd9e11 &client_assertion =eyJhbGci... &grant_type=authorization_code
    • 如果您想从守护程序服务获取令牌,您可以使用客户端凭据流。在此流程中,有两种方法,一种是使用密钥,另一种是使用证书凭证。有关此流程的更多详细信息,请参阅this link
    • 是的,第二种情况(带有证书的访问令牌请求)更适合我的情况。但是在这种情况下,我需要在 POST //oauth2/token 中提供 client_assertion,那么我怎样才能得到这个编码字符串(client_assertion)呢?你有任何 C++ 示例来获取 client_assertion 吗?
    • 由于不熟悉 C++,我通过 C# 在帖子中附加了一个代码示例。但是进度应该是一样的,使用证书登录你创建的 JWT 令牌来生成 client_assertion。更多关于这个进展的细节,你可以参考this link
    • 是否有任何方法可以在不使用 C# 代码或 .Net 库的情况下获取签名的 client_assertion JWT。例如如果我想从邮递员那里调用 Azure AD 令牌 URL,如何获取已签名的 client_assertion JWT?
    猜你喜欢
    • 2020-10-21
    • 2020-09-05
    • 1970-01-01
    • 2014-01-03
    • 2013-03-30
    • 1970-01-01
    • 2011-11-30
    • 2017-07-28
    • 2012-07-23
    相关资源
    最近更新 更多