dotnetcore UserPasswordCredential中的服务结构客户端身份验证不起作用 [英] service fabric client authentication in dotnetcore UserPasswordCredential not working
问题描述
我正在aspnetcore(dotnetcore2.0)应用程序中构建小型服务结构维护,但现在无法识别UserPasswordCredential类。来自此处,这是他们所说的设计。这样,我需要转换以前的代码,以便可以创建有效的FabricClient。如何转换GetAccessToken()方法?
I'm building a small service fabric maintenance in aspnetcore (dotnetcore2.0) application but now doesn't recognize the UserPasswordCredential class. From here it is by design as they say. With that I need to convert my previous code so that I can create a valid FabricClient. How I can convert the GetAccessToken() method?
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;
}
推荐答案
这是如何通过AAD进行身份验证的示例客户编号通过使用 ADAL 。
Here's an example of how to authenticate to AAD with a clientid & clientsecret, by using ADAL.
(这也适用于aspnetcore 2.0)
(this works in aspnetcore 2.0 as well)
基本代码:
var authContext = new AuthenticationContext(authority);
clientCredential = new ClientCredential(clientId, appKey);
result = await authContext.AcquireTokenAsync(resourceId, clientCredential);
这篇关于dotnetcore UserPasswordCredential中的服务结构客户端身份验证不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!