Power BI:System.InvalidOperationException:当应用程序未在

时间:2017-04-25 11:21:04

标签: asp.net-mvc azure iis powerbi powerbi-embedded

我们在IIS中部署项目时遇到以下错误,从visual studio运行时工作正常。

调用以下方法获取AccessToken

时出错
return authContext.AcquireToken(this.PowerBiAPI, this.ClientID, new Uri(this.RedirectUrl)).AccessToken;

错误是:

System.InvalidOperationException: 
Showing a modal dialog box or form when the application is not running in
UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly 
style to display a notification from a service application.
at Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext.RunAsyncTask[T](Task`1 task)
at DSLUI.Controllers.BusinessLogic.ReportService.GetAccessToken() 

此处为App Type:Native

生成this.ClientID

网址:https://dev.powerbi.com/apps

如果有人对上述错误有任何疑问,请帮助我们。

1 个答案:

答案 0 :(得分:0)

因为我正在使用App type Native,AAD popup在服务器端出现导致此问题。

然后我将其更改为App Type to Web app,并使用以下代码获取访问令牌,问题得到解决。

public async Task<string> GetAccessToken()
    {
        try
        {
            var password = Utility.Decrypt(this.PWBI_Psw);

            var content = new FormUrlEncodedContent(new[]
                 {
                 new KeyValuePair<string, string>("grant_type", "password"),
                 new KeyValuePair<string, string>("scope", "openid"),
                 new KeyValuePair<string, string>("username", this.PWBI_User),
                 new KeyValuePair<string, string>("password", password),
                 new KeyValuePair<string, string>("client_id", this.ClientID),
                 new KeyValuePair<string, string>("client_secret", this.ClientSecretKey),
                 new KeyValuePair<string, string>("resource", this.PowerBiAPI)
                 });

            string tokenEndpointUri = string.Format(this.TokenEndpointUri, this.TenantID);
            using (var client = new HttpClient())
            {
                HttpResponseMessage res = client.PostAsync(tokenEndpointUri, content).Result;

                string json = await res.Content.ReadAsStringAsync();

                var obj = JObject.Parse(json);
                var AccessToken = (string)obj["access_token"];

                return AccessToken;
            }
        }
        catch (Exception ex)
        {

            throw ex;
        }
    }