service fabric client authentication in dotnetcore

2019-05-11 18:08发布

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;
    }

1条回答
冷血范
2楼-- · 2019-05-11 18:37

Here's an example of how to authenticate to AAD with a clientid & clientsecret, by using ADAL.

(this works in aspnetcore 2.0 as well)

Essential code:

var authContext = new AuthenticationContext(authority);
clientCredential = new ClientCredential(clientId, appKey);
result = await authContext.AcquireTokenAsync(resourceId, clientCredential);
查看更多
登录 后发表回答