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;
}
推荐答案
这里是 一个示例,说明如何使用 clientid & 向 AAD 进行身份验证.clientsecret,通过使用 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屋!