dotnetcore 中的服务结构客户端身份验证 UserPasswordCredential 不起作用

2024-02-19

我正在 aspnetcore (dotnetcore2.0) 应用程序中构建一个小型服务结构维护,但现在无法识别 UserPasswordCredential 类。从here https://github.com/AzureAD/azure-activedirectory-library-for-dotnet/issues/482正如他们所说,这是设计使然。这样我需要转换以前的代码,以便我可以创建一个有效的 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 https://github.com/Azure-Samples/active-directory-dotnet-daemon/blob/master/TodoListDaemon/Program.cs有关如何使用 clientid 和 clientsecret 对 AAD 进行身份验证的示例,方法是使用ADAL https://www.nuget.org/packages/Microsoft.IdentityModel.Clients.ActiveDirectory/.

(这也适用于 aspnetcore 2.0)

基本代码:

var authContext = new AuthenticationContext(authority);
clientCredential = new ClientCredential(clientId, appKey);
result = await authContext.AcquireTokenAsync(resourceId, clientCredential);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

dotnetcore 中的服务结构客户端身份验证 UserPasswordCredential 不起作用 的相关文章

随机推荐