我已经在Azure活动目录中注册了Dynamics 365应用,现在我正尝试创建一个身份验证令牌,该令牌要与Web API调用一起使用。
我面临的问题是authContext.AcquireTokenAsync方法失败,出现以下异常:
System.NullReferenceException未处理 消息:mscorlib.dll中发生了类型为'System.NullReferenceException'的未处理异常 附加信息:对象引用未设置为对象的实例。
AuthenticationParameters ap = AuthenticationParameters
.CreateFromResourceUrlAsync(new Uri(apiUrl)).Result;
string resourceUrl = ap.Resource;
string authorityUrl = ap.Authority;
authContext = new AuthenticationContext(authorityUrl, false);
//User Credentials from the AppConfig
UserCredential credentials =
new UserPasswordCredential("myusername", "password");
//Generate the AuthToken by using Credentials object.
authToken = await authContext
.AcquireTokenAsync(resourceUrl, applicationId, credentials);
请注意,authContext
不是null
,所有参数都不是null
。