我有一个.net核2.0 asp.net的MVC Web应用程序。出于同样的我有一个.net 2.0标准单元测试项目。对于单元测试所写的,我得叫Azure的AD受保护的Web API。任何人都可以让我知道,我们怎样才能从单元测试项目在.NET 2.0标准蔚蓝广告访问令牌。
这是可能的.NET Framework中,因为它具有“UserPasswordCredential”中的“Microsoft.IdentityModel.Clients.ActiveDirectory” DLL提供的类。但这个类是在.net标准2.0(https://github.com/AzureAD/azure-activedirectory-library-for-dotnet/issues/482)除去
我用这样的集成测试:
string tokenUrl = _authority + "oauth2/token";
var req = new HttpRequestMessage(HttpMethod.Post, tokenUrl)
{
Content = new FormUrlEncodedContent(new Dictionary<string, string>
{
["grant_type"] = "password",
["client_id"] = settings.ClientId,
["client_secret"] = settings.ClientSecret,
["resource"] = _resourceUri,
["username"] = settings.UserName,
["password"] = settings.Password
})
};
HttpResponseMessage res = await _client.SendAsync(req);
string json = await res.Content.ReadAsStringAsync();
AadTokenResponse tokenResponse = JsonConvert.DeserializeObject<AadTokenResponse>(json);
有几类级别字段,如AAD权威,API资源URI和HttpClient的。
那么,这样做是获得使用资源所有者密码凭据授予流程的访问令牌。这是那些少数情况下,使用这种流实际上是有意义的一个。我们收购一个用户的上下文的访问令牌没有登录窗口。当更好的东西是可用的,在这种情况下还要求用户不是联盟这种流动不应该使用,不具备MFA等。
你可能会想缓存令牌,这样你就不会从你的测试锤令牌端点毫无意义。