如何使用客户端密钥对Microsoft动态进行身份验证?

时间:2017-11-29 21:27:59

标签: c# .net authentication microsoft-dynamics dynamics-crm-365

我正在尝试使用应用的客户端ID和应用程序密钥对Dynamics进行身份验证。这在Visual Studio中的Web应用程序项目中失败(本地调试)。下面是我正在尝试使用的代码。看起来我正在获取一个身份验证令牌以传入,但它会抛出此错误:

HTTP请求未经授权,客户端身份验证方案为“匿名”。从服务器收到的身份验证标头是'Bearer authorization_uri = https://login.windows.net/ ....

        string organizationUrl = "https://myurl.dynamics.com";
        string clientId = "myclientid";
        string appKey = "myclientsecret";
        string aadInstance = "https://login.microsoftonline.com/";
        string tenantID = "mytenantid";
        ClientCredential clientcred = new ClientCredential(clientId, appKey);
        AuthenticationContext authenticationContext = new AuthenticationContext(aadInstance + tenantID);
        AuthenticationResult authenticationResult = authenticationContext.AcquireToken(organizationUrl, clientcred);
        var requestedToken = authenticationResult.AccessToken;

        using (var sdkService = new OrganizationWebProxyClient(GetServiceUrl(organizationUrl), false))
        {
            sdkService.HeaderToken = requestedToken;
            OrganizationRequest request = new OrganizationRequest()
            {
                RequestName = "WhoAmI"
            };
            WhoAmIResponse response = sdkService.Execute(new WhoAmIRequest()) as WhoAmIResponse;
            Console.WriteLine(response.UserId);
        }

0 个答案:

没有答案