問(wèn)題描述
我們正在編寫(xiě)一個(gè) WCF 服務(wù),該服務(wù)必須與 Dynamics CRM 2016 Online 集成.我正在嘗試使用 ADAL 進(jìn)行身份驗(yàn)證,使用方法 AcquireTokenAsync()
.問(wèn)題是,它會(huì)顯示一個(gè)彈出框,提示用戶輸入憑據(jù).當(dāng)然,我們的應(yīng)用程序是一項(xiàng)服務(wù),這不是我們想要的.我們一直在尋找一種無(wú)需此彈出框即可進(jìn)行身份驗(yàn)證的方法.
We are writing a WCF service which has to integrate with Dynamics CRM 2016 Online. I'm trying to authenticate using ADAL, using method AcquireTokenAsync()
. Problem is, it displays a pop-up box, prompting the user for credentials. Naturally, our application being a service, this isn't what we want. We've been searching for a way to authenticate without having this pop-up box.
有一個(gè)名為 AuthenticationContextIntegratedAuthExtensions
的類,它應(yīng)該幫助用戶名/密碼流".它有一個(gè)方法 AcquireTokenAsync
,它抑制了彈出框,但我們還沒(méi)有找到任何方法來(lái)將密碼傳遞給它.當(dāng)只使用用戶名運(yùn)行時(shí),它會(huì)引發(fā)異常,基本上說(shuō)沒(méi)有提供密碼".
There is a class called AuthenticationContextIntegratedAuthExtensions
, which is supposed to assist with "username/password flow". It has the single method AcquireTokenAsync
, which suppresses the pop-up box, but we haven't found any way to pass the password to it. When run with just the username, it raises the exception that basically says "no password was supplied".
有誰(shuí)知道如何解決這個(gè)問(wèn)題?甚至不必是 ADAL.只是為了獲取 OAuth 令牌.
Does anyone have any idea how to work around this? Doesn't even have to be ADAL. Just something to acquire the OAuth token.
推薦答案
private static string API_BASE_URL = "https://<CRM DOMAIN>.com/";
private static string API_URL = "https://<CRM DOMAIN>.com/api/data/v8.1/";
private static string CLIENT_ID = "<CLIENT ID>";
static void Main(string[] args)
{
var ap = AuthenticationParameters.CreateFromResourceUrlAsync(
new Uri(API_URL)).Result;
var authContext = new AuthenticationContext(ap.Authority, false);
var userCredential = new UserCredential("<USERNAME>", "<PASSWORD>");
var result = authContext.AcquireToken(API_BASE_URL, CLIENT_ID, userCredential);
var httpClient = HttpWebRequest.CreateHttp(Path.Combine(API_URL, "accounts"));
httpClient.Headers.Add(HttpRequestHeader.Authorization, "Bearer:" + result.AccessToken);
using (var sr = new StreamReader(httpClient.GetResponse().GetResponseStream()))
{
Console.WriteLine(sr.ReadToEnd());
}
}
注意:我使用的是舊版 ADAL (2.19.208020213),因?yàn)槊艽a參數(shù)已從 UserCredential 構(gòu)造函數(shù)中取出.
Note: I'm using an older version of ADAL (2.19.208020213) as it appears the password parameter has been taken out of the UserCredential constructor.
最新版本的 ADAL 具有 UserPasswordCredential 可以代替 UserCredential
使用(并且可能在 Password
已從 UserCredential
中刪除)
Latest versions of ADAL have UserPasswordCredential which can be used in place of UserCredential
(and probably was added as soon as Password
was removed from UserCredential
)
編輯 2: CRM 現(xiàn)在支持 服務(wù)器到服務(wù)器身份驗(yàn)證,它允許您創(chuàng)建應(yīng)用程序用戶.
EDIT 2: CRM now supports Server to Server Authentication which allows you to create an application user.
這篇關(guān)于C# ADAL AcquireTokenAsync() 沒(méi)有彈出框的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!