我正在aspnetcore(dotnetcore2.0)应用程序中构建小型服务结构维护,但现在无法识别UserPasswordCredential类。正如他们所说,从here开始是设计使然。这样,我需要转换之前的代码,以便可以创建有效的FabricClient。如何转换GetAccessToken()方法?
private static FabricClient CreateSecuredFabricClient()
{
try
{
var claimsCredentials = new ClaimsCredentials();
claimsCredentials.ServerThumbprints.Add(_sslThumbprint);
var fc = new FabricClient(claimsCredentials, _clusterConnectionString);
fc.ClaimsRetrieval += (o, e) =>
{
var token = GetAccessToken(e.AzureActiveDirectoryMetadata);
return token.Result.AccessToken;
};
return fc;
}
catch (FabricException fex)
{
Console.WriteLine("Connect failed: {0}", fex.InnerException.Message);
}
catch (Exception e)
{
Console.WriteLine("Connect failed: {0}", e.Message);
}
return null;
}
private static Task<AuthenticationResult> GetAccessToken(System.Fabric.Security.AzureActiveDirectoryMetadata aad)
{
var authContext = new AuthenticationContext(aad.Authority);
var authResult = authContext.AcquireTokenAsync(
aad.ClusterApplication,
aad.ClientApplication,
new UserPasswordCredential(_username, _password));
return authResult;
}
最佳答案
Here's有关如何使用ADAL通过clientid和clientsecret向AAD进行身份验证的示例。
(这也适用于aspnetcore 2.0)
基本代码:
var authContext = new AuthenticationContext(authority);
clientCredential = new ClientCredential(clientId, appKey);
result = await authContext.AcquireTokenAsync(resourceId, clientCredential);