简体   繁体   English

如何获得 Dynamics 365 令牌?

[英]How to get Dynamics 365 Token?

I trying to get Dynamics 365 token but the method for obtain the token is asynchronous and when I try to return the token I obtain a 500 Internal Server Error.我试图获取 Dynamics 365 令牌,但获取令牌的方法是异步的,当我尝试返回令牌时,我获得了 500 内部服务器错误。 Is there another way to get Dynamics 365 token?还有其他方法可以获得 Dynamics 365 令牌吗? Or how can I show the token on return of the controller?或者如何在控制器返回时显示令牌?

My get token method:我的获取令牌方法:

 public static async Task<string> AccessTokenGenerator()
    {
        string clientId = "myClientId";
        string clientSecret = "myClientSecret";
        string authority = "https://login.microsoftonline.com/9dfdf489-e992-46ea-9fc7-317dfc206add";
        string resourceUrl = "https://devtreinamento.crm2.dynamics.com"; // Org URL  

        ClientCredential credentials = new ClientCredential(clientId, clientSecret);
        var authContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(authority);
        var result = await authContext.AcquireTokenAsync(resourceUrl, credentials);
        return result.AccessToken;
    }

My show token method:我的表演令牌方法:

[Route ("")]
    [HttpGet]
    public  ActionResult showToken()
    {
        var token = getToken.AccessTokenGenerator();

        return Ok(token);
    }

Change your "Get" method to async and return a Task<T> of type T ActionResult , then await your call to AccessTokenGenerator() , in your current code, you are setting a variable equal to an async method without awaiting the async method to complete.:将您的“Get”方法更改为async并返回 T ActionResult类型的Task<T> ,然后await您对AccessTokenGenerator()调用,在您当前的代码中,您正在设置一个等于async方法的变量,而无需等待async方法完全的。:

[Route ("")]
[HttpGet]
public async Task<ActionResult> showToken()
{
    var token = await getToken.AccessTokenGenerator();
    return Ok(token);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM