C# ADAL AcquireTokenAsync() without pop-up box

前端 未结 3 1618
不思量自难忘°
不思量自难忘° 2021-01-02 17:06

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(). Pro

3条回答
  •  一个人的身影
    2021-01-02 17:20

    private static string API_BASE_URL = "https://.com/";
    private static string API_URL = "https://.com/api/data/v8.1/";
    private static string 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("", "");
    
        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());
        }
    }
    

    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.

    EDIT: 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)

    EDIT 2: CRM now supports Server to Server Authentication which allows you to create an application user.

提交回复
热议问题