Cordovaアプリケーションは、社内のADFS(3.0)からセキュリティトークンを要求する必要があります。次に、トークンを使用してWebサービスに接続します。私が見つけたすべての例ではこれが可能だと言われていますが、Azureを使用する方法だけを示しています。ADFS 3.0 + Cordova Mobile App + API
ADFS 3.0の詳細な設定情報はどこにありますか?より良いアプローチが存在するか?
Cordovaアプリケーションは、社内のADFS(3.0)からセキュリティトークンを要求する必要があります。次に、トークンを使用してWebサービスに接続します。私が見つけたすべての例ではこれが可能だと言われていますが、Azureを使用する方法だけを示しています。ADFS 3.0 + Cordova Mobile App + API
ADFS 3.0の詳細な設定情報はどこにありますか?より良いアプローチが存在するか?
私は今日この問題を解決しました。ここに実際の例があります。それはCordovaだけでなく、すべてのモバイルアプリで動作するはずです。
string adfsHost = "https://<Your ADFS FQDN>";
string sendTo = $"{adfsHost}/adfs/services/trust/13/usernamemixed";
string _username = "<Your Domain\\<Your username>";
string _password = "<Your password>";
string applyTo = "<Your Resource URI>";
string tokenType = "urn:ietf:params:oauth:token-type:jwt";
string soapMessage = [email protected]"
<s:Envelope xmlns:s=""http://www.w3.org/2003/05/soap-envelope""
xmlns:a=""http://www.w3.org/2005/08/addressing""
xmlns:u=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"">
<s:Header>
<a:Action s:mustUnderstand=""1"">http://docs.oasis-open.org/ws-sx/ws-trust/200512/RST/Issue</a:Action>
<a:To s:mustUnderstand=""1"">{sendTo}</a:To>
<o:Security s:mustUnderstand=""1"" xmlns:o=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"">
<o:UsernameToken u:Id="" uuid-00000000-0000-0000-0000-000000000000-0"">
<o:Username>{_username}</o:Username>
<o:Password Type=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"">{_password}</o:Password>
</o:UsernameToken>
</o:Security>
</s:Header>
<s:Body>
<trust:RequestSecurityToken xmlns:trust=""http://docs.oasis-open.org/ws-sx/ws-trust/200512"">
<wsp:AppliesTo xmlns:wsp=""http://schemas.xmlsoap.org/ws/2004/09/policy"">
<a:EndpointReference>
<a:Address>{applyTo}</a:Address>
</a:EndpointReference>
</wsp:AppliesTo>
<trust:KeyType>http://docs.oasis-open.org/ws-sx/ws-trust/200512/Bearer</trust:KeyType>
<trust:RequestType>http://docs.oasis-open.org/ws-sx/ws-trust/200512/Issue</trust:RequestType>
<trust:TokenType>{tokenType}</trust:TokenType>
</trust:RequestSecurityToken>
</s:Body>
</s:Envelope>
";
XmlDocument xml = new XmlDocument();
xml.LoadXml(soapMessage);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(sendTo);
request.ContentType = "application/soap+xml; charset=utf-8";
request.Method = "POST";
request.Accept = "application/json";
var stream = request.GetRequestStream();
xml.Save(stream);
WebResponse response = request.GetResponse();
string strResponse;
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader sr = new StreamReader(responseStream, System.Text.Encoding.ASCII))
{
strResponse = sr.ReadToEnd();
}
}
JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler();
XmlDocument xml2 = new XmlDocument();
xml2.LoadXml(strResponse);
XmlNode node = xml2.SelectSingleNode("//*[@ValueType='urn:ietf:params:oauth:token-type:jwt']");
XmlReader reader = new XmlNodeReader(node);
JwtSecurityToken token = (JwtSecurityToken)handler.ReadToken(reader);
string encryptedToken = token.RawData;
このコードは、モバイルアプリのユーザー資格情報を一番上に受け取ることをシミュレートしています。次に、ADFS 3.0を呼び出すために必要な残りの値をセットアップします。値は文字列補間を使用してSOAPエンベロープに挿入されます。
次に、Webリクエストを作成します。次に、SOAPエンベロープを要求に追加し、ADFSエンドポイントを呼び出します。 BinarySecurityTokenとステータスコード200を含むSOAP応答を受け取る必要があります。
JWTトークンはBinarySecurityToken内にラップされています。それを取得するには、それを含むwsse:BinarySecurityTokenを選択し、JwtSecurityTokenHandler.ReadToken()を使用して取り出します。その後、トークンをモバイルアプリに送信して、モバイルアプリでAPIリクエストを完了することができます。
携帯電話から直接ADFSを呼び出すのと同じアプローチを使用することもできますが、私はAPI側でそれを行うことをお勧めします。
また、自己署名証明書を使用しないことを強くお勧めします。 IMHOでは、ADFSのやりとりの多くは単純に機能しません。ただ頭痛を払わないでください。