2017-02-08 3 views
0

ClientAssertionCertificateを使用してADFS(Windows server 2012R2)からJWTを取得できません。CでClientAssertionCertificateを使用してADFS(Windows server 2012R2)からJWTを取得する方法

ユーザーが自分のユーザー名とパスワードで認証してadfsログインウィンドウに直接入れることはできますが、自分のアプリケーションのユーザーがADFSログインウィンドウを表示しないようにしたい資格情報を知っていません。私のアプリケーションは実際には(APIの)ユーザーであるため、証明書を使用して認証したい。私はそれをやってみたとき、私は、認証サーバは、要求されたgrant_typeサポートしていません」というメッセージが表示されます。認証サーバのみauthorization_codeや補助金型としてrefresh_tokenサポートしています。

誰もが任意の回避策やJWTを得るために他の方法を知っています?ADFSは、証明書を使用してから多くのことを、THX

私のアプリケーションは、ネット4.6.1コンソールアプリケーションです

は、これが今の私のコードです:!。

var certPath = Path.Combine(GetCurrentDirectoryFromExecutingAssembly(), "mycertificate.pfx"); 
     var certfile = File.OpenRead(certPath); 
     var certificateBytes = new byte[certfile.Length]; 
     certfile.Read(certificateBytes, 0, (int)certfile.Length); 
     var cert = new X509Certificate2(
      certificateBytes, 
      "mypassword", 
      X509KeyStorageFlags.Exportable | 
      X509KeyStorageFlags.MachineKeySet | 
      X509KeyStorageFlags.PersistKeySet); 

     var certificate = new ClientAssertionCertificate("myclientid", cert); 

     AuthenticationContext context = new AuthenticationContext("https://sts.example.com/adfs",false); 
     AuthenticationResult authenticationResult = await context.AcquireTokenAsync("http://example.com/api", certificate); 
     var token = authenticationResult.AccessToken; 

答えて

2

WSTrustChannelFactoryをCertificateWSTrustBindingとともに使用しようとしましたか? RequestSecurityTokenでは、JWTが必要なTokenTypeを指定できます。受け取ったJWTは、デコードする必要があるbase64文字列になります。

public static async Task<string> GetAccessToken(string authority, string resource, string clientId) 
    { 


     var certPath = Path.Combine(GetCurrentDirectoryFromExecutingAssembly(), "mycertificate.pfx"); 
     var certfile = File.OpenRead(certPath); 
     var certificateBytes = new byte[certfile.Length]; 
     certfile.Read(certificateBytes, 0, (int)certfile.Length); 
     var cert = new X509Certificate2(
      certificateBytes, 
      "PASSWORD", 
      X509KeyStorageFlags.Exportable | 
      X509KeyStorageFlags.MachineKeySet | 
      X509KeyStorageFlags.PersistKeySet); 



     var factory = new WSTrustChannelFactory(
      new CertificateWSTrustBinding(
       SecurityMode.TransportWithMessageCredential), 
      "https://example.com/adfs/services/trust/13/certificatemixed") {TrustVersion = TrustVersion.WSTrust13}; 


     if (factory.Credentials != null) 
      factory.Credentials.ClientCertificate.Certificate = cert; 

     // create token request 
     var rst = new RequestSecurityToken 
     { 
      RequestType = RequestTypes.Issue, 
      KeyType = KeyTypes.Bearer, 
      AppliesTo = new EndpointReference("http://example.com/api"), 
      KeySizeInBits = 0, 
      TokenType = "urn:ietf:params:oauth:token-type:jwt" 
     }; 

     // request token and return 
     var genericXmlSecurityToken = factory.CreateChannel().Issue(rst) as GenericXmlSecurityToken; 
     return genericXmlSecurityToken != null 
      ? Encoding.UTF8.GetString(Convert.FromBase64String(genericXmlSecurityToken.TokenXml.InnerXml)) 
      : string.Empty; 
    } 
+0

Thx Tommy!できます!私は過去5日間これを探しています! –

関連する問題