C# ADAL AcquireTokenAsync() 没有弹出框

时间:2023-02-18
本文介绍了C# ADAL AcquireTokenAsync() 没有弹出框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在编写一个 WCF 服务,该服务必须与 Dynamics CRM 2016 Online 集成.我正在尝试使用 ADAL 进行身份验证,使用方法 AcquireTokenAsync().问题是,它会显示一个弹出框,提示用户输入凭据.当然,我们的应用程序是一项服务,这不是我们想要的.我们一直在寻找一种无需此弹出框即可进行身份验证的方法.

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.

有一个名为 AuthenticationContextIntegratedAuthExtensions 的类,它应该帮助用户名/密码流".它有一个方法 AcquireTokenAsync,它抑制了弹出框,但我们还没有找到任何方法来将密码传递给它.当只使用用户名运行时,它会引发异常,基本上说没有提供密码".

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".

有谁知道如何解决这个问题?甚至不必是 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),因为密码参数已从 UserCredential 构造函数中取出.

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 现在支持 服务器到服务器身份验证,它允许您创建应用程序用户.

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

这篇关于C# ADAL AcquireTokenAsync() 没有弹出框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:Web 应用程序使用窗口域帐户进行身份验证 下一篇:从活动目录获取用户的密码

相关文章

最新文章