【问题标题】:AuthenticationContext.AcquireToken equivalent in .NET 3.5.NET 3.5 中的 AuthenticationContext.AcquireToken 等效项
【发布时间】:2017-06-24 11:09:29
【问题描述】:

我有一个在 .Net Framework 4.5 中工作的代码,但我需要在 .Net 3.5 中的等效代码。我的问题是,我几乎所有的谷歌搜索结果要么是使用新 WIF 的解决方案,要么是关于旧 WIF 3.5 的一般信息。

代码如下所示:

using Microsoft.IdentityModel.Clients.ActiveDirectory;

namespace x 
{
    class y 
    {
        public string GetAuthenticationHeader(Ax7Config config)
        {
            var user = new UserCredential(config.username, config.password);
            return new AuthenticationContext(config.tenant)
                .AcquireToken(config.resource, config.clientAppId, user)
                .CreateAuthorizationHeader();
        }
    }
}

PS: 生成的 dll 会作为插件导入在 3.5 .net 框架上运行的应用程序中,并且无法重新编译为最新的框架。所以这行不通。

附: 对于它的价值,我知道.CreateAuthorizationHeader() 只会返回"Bearer " + AccessToken。所以这不是问题。获取AccessToken是。

【问题讨论】:

    标签: c# security wif bearer-token


    【解决方案1】:

    最后,AcquireToken 只是向您的 STS 发送一个 https 请求。您可以自己轻松地进行模拟。请求是这样的(对于 AAD):

    POST https://login.microsoftonline.com/your-tenant-id/oauth2/token HTTP/1.1
    Accept: application/json
    x-client-Ver: 3.13.5.907
    x-client-CPU: x64
    x-client-OS: Microsoft Windows NT 6.2.9200.0
    x-ms-PKeyAuth: 1.0
    client-request-id: 10a9f6d3-1247-493e-874f-fab04e1427c7
    return-client-request-id: true
    Content-Type: application/x-www-form-urlencoded
    Host: login.microsoftonline.com
    Content-Length: 183
    Expect: 100-continue
    Connection: Keep-Alive
    
    resource=your-resource-guid&client_id=your-lcient-guid&client_secret=***** CREDENTIALS REMOVED HERE *****&grant_type=client_credentials
    

    这很容易通过 WebClient (p.e; How to fill forms and submit with Webclient in C#) 来实现。服务器的响应通常是这样的:

    HTTP/1.1 200 OK
    Cache-Control: no-cache, no-store
    ...
    Content-Type: application/json; charset=utf-8
    Expires: -1
    Server: Microsoft-IIS/8.5
    Strict-Transport-Security: max-age=31536000; includeSubDomains
    X-Content-Type-Options: nosniff
    client-request-id: 10a9f6d3-1247-493e-874f-fab04e1427c7
    x-ms-request-id: bla-bla
    …
    X-Powered-By: ASP.NET
    Date: Thu, 23 Feb 2017 08:35:26 GMT
    Content-Length: 1278
    
    {"token_type":"Bearer","expires_in":"3599","ext_expires_in":"10800","expires_on":"1487842528","not_before":"1487838628","resource":"your-resource-id","access_token":"your-access-token"}
    

    结果是一个 json 并且令牌在“access_token”字段中。您可以使用 Fiddler 之类的工具来正确处理您的请求,但这基本上就是它的功能。 (您可能会使用 Newtonsoft 正确反序列化 json。)

    不要让我说这就是 ADAL 为您所做的一切。 ADAL 还做诸如令牌缓存之类的事情,因此您不必在每次调用时都请求令牌,它会自动处理过期等。但是您也可以使用一些代码自己滚动。 希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      下面是我用来获取令牌的代码。结果将在 json 中,您必须对其进行反序列化才能读取“access_token”字段。

      HttpClient client = new HttpClient();
      var values = new Dictionary<string, string>
                      {
                         { "resource", "xxx" },
                         { "client_id", "xxx" },
                      { "client_secret","xxx"},
                      {"client_info","1" },
                      {"grant_type","client_credentials" }
                      };
      
      var content = new FormUrlEncodedContent(values);
      
      var response = client.PostAsync("https://login.microsoftonline.com/contoso.com/oauth2/token", content).Result;
      
      var responseString = response.Content.ReadAsStringAsync().Result;
      
      return responseString;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-21
        • 2015-10-30
        • 1970-01-01
        • 2010-09-20
        • 2010-10-19
        相关资源
        最近更新 更多