我想使用应用程序远程连接到Service Fabric。目前,我们使用用户名/密码进行连接,这不是很安全。如果我运行下面的代码,它将返回给定的应用程序不是Service Fabric集群的管理员。我似乎无法做的是添加一个应用程序作为管理员,只添加用户。有没有人这样做过?关于它的在线很少。
string token = GetAccessToken(tenantID, clusterApplicationId, clusterSecret);
var claimsCredentials = new ClaimsCredentials();
claimsCredentials.ServerThumbprints.Add(serverCertThumb);
claimsCredentials.LocalClaims = token;
try
{
var fc = new FabricClient(claimsCredentials, connection);
var ret = fc.ClusterManager.GetClusterManifestAsync().Result;
Console.WriteLine(ret.ToString());
}
catch (Exception e)
{
Console.WriteLine("Connect failed: {0}", e.Message);
}
private static string GetAccessToken(string tenantId, string clientId, string secretKey)
{
string resource = "https://graph.microsoft.com";
string authorityFormat = @"https://login.microsoftonline.com/{0}";
string authority = string.Format(CultureInfo.InvariantCulture, authorityFormat, tenantId);
var authContext = new AuthenticationContext(authority);
var clientCredential = new ClientCredential(clientId, secretKey);
var authResult = authContext.AcquireTokenAsync(resource, clientCredential).Result;
return authResult.AccessToken;
}
如果您使用应用程序通过使用client credential flow获取access_token,它将使用自己的凭据而不是模拟用户,以便在调用其他Web服务时进行身份验证。在azure门户中,您可以将角色分配给应用程序,或者仅通过委派权限来模拟用户。